I am new in PHP. I have my URL like below
http://localhost/index.php?page=20
http://localhost/user.php?id=15&localtion=delhi
http://localhost/folder/profile.php?id=15&active=today
What I am looking for convert my URL like below
http://localhost/page/20
http://localhost/user/id/15/localtion/delhi
http://localhost/folder/profile/id/15/active/today
I am trying from many hours to make it working with .htaccess file but no luck yet
one of little working code is like below
RewriteEngine on
RewriteRule ^(.+)\.php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]
its removing .php from URL like below
http://localhost/index?page=15
I have checked many questions and answer in stackoverflow like below
https://stackoverflow.com/questions/41390397/htaccess-seo-friendly-url
https://stackoverflow.com/questions/41439512/php-url-replace-question-mark-and-parameter-with-slash
https://stackoverflow.com/questions/12451886/htaccess-for-friendly-url-with-multiple-variables
but not able to achieve my goal. Let me know if anyone expert here can help me for doing same and still my php code work fine without any issue.
Thanks a lot!
With your shown samples, please try following htaccess rules. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Rule for external rewrite.
##Rule to handle from http://localhost/index.php?page=20 to http://localhost/page/20
RewriteCond %{THE_REQUEST} \s/index\.php\?([\w-]+)=(\d+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
##Rule for external rewrite.
##Rule to handle from http://localhost/user.php?id=15&localtion=delhi TO http://localhost/user/id/15/localtion/delhi
RewriteCond %{THE_REQUEST} \s/(user)\.php\?([\w-]+)=(\d+)&([\w-]+)=([\w-]+)\s [NC]
RewriteRule ^ /%1/%2/%3/%4/%5? [R=301,L]
##Rule for external rewrite.
##Rule to handle from http://localhost/folder/profile.php?id=15&active=today To http://localhost/folder/profile/id/15/active/today
RewriteCond %{THE_REQUEST} \s/(folder)/(profile)\.php\?([\w-]+)=(\d+)&([\w-]+)=([\w-]+)\s [NC]
RewriteRule ^ /%1/%2/%3/%4/%5/%6? [R=301,L]
##internal rewrite rule to handle files internally.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ $1/$2.php?$3=$4&$6=$6 [QSA,L]
##internal rewrite rule to handle files internally.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ $1.php?$2=$3&$4=$5 [QSA,L]
##internal rewrite rule to handle files internally.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ index.php?$1=$2 [QSA,L]
Related
I apologise if this question is asked before but I can't find the specific instance for my problem.
How to achieve this www.example.com/index.php?user=john to www.example.com/john
I am using this already to remove the .php extension but it doesn't fix the user parameter.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R]
ErrorDocument 404 /error.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/([A-Za-z0-9_]+) ?user=$1
I highly value any reply. Please help.
With your shown attempts, please try following htaccess rules. Considering that you have to pass your arguments to index.php file in backend.
Also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect rules here.
RewriteCond %{THE_REQUEST} \s/index\.php\?user=(\S+)\s [NC]
RewriteRule ^ /%1? [R=301,L]
##Internal rewrite rules here..
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ index.php?user=$1 [QSA,L]
I have my URL https://example.com/?hfgshdsds.
I need to rewrite a rule to makes that even If I removed the question mark , the link will works as same as before, so my url need to be https://example.com/hfgshdsds.
For the moment on my .htaccess file I have only the rule that open the php without extension .php .
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
Thank you!
With your shown samples, please try following htaccess Rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine On
##keep redirection Rules first then keep rest of rules.
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php/?$ %{REQUEST_FILENAME}.php [QSA,L]
##Adding new rule here for non-existing pages here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?$1 [QSA,L]
I have made sure that rewrite engine is enabled and removing .php extensions is working so I know that isn't the issue.
what I'm trying to do is simply remove the ?id=value aspect of the URL, so basically making the URL look like such:
folder/medias/value
Instead of
folder/medias?id=value
My current .htaccess looks like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteRule ^404/?$ /404.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ 404.php [L,R]
With your shown samples/attempts, please try following htaccess Rules. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Rules for external rewrite.
RewriteCond %{THE_REQUEST} \s/([^.]*)\.php\?id=(\S+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
##Rule for internal rewrite.
RewriteRule ^([^/]*)/([^/]*)/?$ $1?id=$3 [L]
You may try this code inside the /folder/.htaccess (create this file if it doesn't exist):
RewriteEngine On
# External redirect from /folder/media?id=val to /folder/media/val
RewriteCond %{THE_REQUEST} /(\S+?)\.php\?id=([^&\s]+)\s [NC]
RewriteRule ^ /folder/%1/%2? [R=301,L,NE]
# Internal rewrite from /folder/media/val to /folder/media?id=val
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/([\w-]+)/?$ $1.php?id=$2 [L,QSA]
Trailing ? in first rule is to remove query string from original URL.
%{REQUEST_FILENAME} !-f and %{REQUEST_FILENAME} !-d is to skip existing files and directories from rewrite in 2nd rule.
original url - example.com/stars.php?id=9&s=lorem-ipsum
want to be - example.com/stars/9/lorem-ipsum
here is my try - getting error 500
RewriteCond %{THE_REQUEST} \s/stars\.php\?id=([^&]*)&s=([^\s&]*) [NC]
RewriteRule ^ /%1/%2? [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)\.php$ /stars.php?id=$1&s=$2 [L]
.htaccess and stars.php are placed in the root - example.com
pls help
With your shown samples, could you please try following. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Rule to handle external redirection to user friendly URL here.
RewriteCond %{THE_REQUEST} \s/(stars)\.php\?id=(\d+)&s=([^\s]*) [NC]
RewriteRule ^ %1/%2/%3? [R=301,L]
##Rule to handle non-existing files/directories should be served by php files in backend.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ $1.php?id=$2&s=$3 [L]
I've been using following code to achieve pretty urls:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ index.php?p=$1 [L]
And it is working fine.
However, I also need to force my domain to redirect to www while keeping pretty urls.
I've read through some threads that ask for redirection to www, but I am unable to implement it together with pretty url (domain.com -> www.domain.com, domain.com/news -> www.domain.com/news respectively).
Can anyone help me achieve this?
Have www redirect rule before rewrite rule you already have:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/?$ index.php?p=$1 [L,QSA]