How to make URL friendly with . htaccess with Variable in URL

Asked

Viewed 924 times

0

Example I have link like this:

https://meusite.com/linkdireto/? id=999

You can access it like this:

https://meusite.com/linkdireto/999

So keep the URL but clean, I know you have how to do, but I have no idea!

My current . htaccess:

# override max php upload settings. Might not work on all servers
#php_value upload_max_filesize 4000M
#php_value post_max_size 4000M

# setup xsendfile if the module is enabled
<IfModule mod_xsendfile.c>
  <Files *.php>
    XSendFile On
    SetEnv MOD_X_SENDFILE_ENABLED 1
  </Files>
</IfModule>

# disable mod security
#<IfModule mod_security.c>
#    SecFilterEngine Off
#    SecFilterScanPOST Off
#</IfModule>

# redirect www to non-www
#RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
#RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

RewriteEngine On

# Redirecionar para HTTPS
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#RewriteRule . - [E=no-gzip:1]
RewriteBase /

# forward to install if it exists
#RewriteCond %{DOCUMENT_ROOT}/install -d
#RewriteCond %{REQUEST_URI} !(install) [NC]
#RewriteRule ^(.*) /install/ [L,redirect=302]

# forward app requests
#RewriteCond %{HTTP:Authorization} ^(.+)$
#RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteRule ^app/(.*) plugins/webdav/site/control/$1 [QSA,L]

# forward api requests
RewriteRule ^api/v2/(.*)$ api/v2/index.php?_page_url=$1 [QSA,NC,L]

# route everything via index.php if it doesn't exist as a file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?_page_url=$1 [QSA]

1 answer

1


In your htaccess just do the following:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    RewriteRule ^linkdireto/([0-9]+)?$ meuScript.php?id=$1 [NC,L]
</IfModule>

[0-9] in case your id is only with numbers

  • What if it has letters and numbers ? Both..

  • you could do [a-zA-Z0-9] so you will recognize numbers and letters in uppercase or minuscule letters

  • tested and error 404

  • I modified my question my current code to see if it helps..

  • You should put all the Rewrite Ules inside Ifmodule, as I don’t know what the structure of your files is difficult to help, if it gave error 404 then you pointed to the wrong file.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.