I want to implement a RewriteRule in my .htaccess to enable the following URL structure:
http://example.com/([a-zA-Z\.]*) -> user.php?username=$1
I tried it like this:
RewriteRule ^([a-zA-Z\.]*)$ user.php?username=$1 [L,QSA]
If I go to localhost/victorbarbu, it will redirect to user.php?username=victorbarbu, but if I go to storage/, it will still go to user.php. What can I do to avoid this type of redirection?
This is my current .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{THE_REQUEST} \ /+subdir/
RewriteRule ^ - [L]
Try this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php !-f
RewriteRule ^([a-zA-Z.]*)$ user.php?username=$1 [L,QSA]
Related
I want to remove question mark from two URLs like this:
URL 1
http://localhost:8888/events/event.php?id=222
to
http://localhost:8888/events/event/222
URL 2
http://localhost:8888/membership/register.php?id=162
to
http://localhost:8888/membership/register/162
i'm use this code in .htaccess and just the 'event' page work:
Options -MultiViews
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /events/event(?:\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /events/event/%1? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^events/event/([^.]+?)/?$ events/event.php?id=$1 [QSA,L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /membership/register(?:\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /membership/register/%1? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^membership/register/([^.]+?)/?$ membership/register.php?id=$1 [QSA,L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]
You can use:
Options -MultiViews
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /events/event(?:\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /events/event/%1? [R=302,L,NE]
RewriteCond %{THE_REQUEST} /membership/register(?:\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /membership/register/%1? [R=302,L,NE]
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# rewrite from pretty URL
RewriteRule ^events/event/([^.]+?)/?$ events/event.php?id=$1 [QSA,L,NC]
RewriteRule ^membership/register/([^.]+?)/?$ membership/register.php?id=$1 [QSA,L,NC]
# Rewrite with .php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]
I have three four pages
page.php?s=defaultpage
page.php?p=defaultpage
blog.php?post=defaultpage
projects.php?view=defaultpage
I have this exisiting URL redirect code snippet. The fourth page is new, and I would like to write a condition for that to only show projects/defaultpage
How do I achieve this with the existing Rewrite condition.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Options -MultiViews
RewriteEngine On
RewriteBase /Renu/
RewriteCond %{THE_REQUEST} /page\.php\?([a-z])=([^&\s]+)
RewriteRule ^ %1/%2? [R=302,NE,L]
RewriteCond %{THE_REQUEST} /blog\.php\?post=([^&\s]+) [NC]
RewriteRule ^ blog/%1? [R=302,NE,L]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteRule ^blog/([^/]+)/?$ blog.php?post=$1 [L,NC,QSA]
RewriteRule ^([a-z])/([^/.]+)/?$ page.php?$1=$2 [L,QSA]
Just add this line, before the last line:
RewriteRule ^projects/([^/]+)/?$ project.php?view=$1 [L,NC,QSA]
RewriteRule (.)/(.)/(.*)/$ /?view=$1&$2=$3
in php with get is :
example.com/?view=profile&nick=NickName
with .htaccess
example.com/profile/NickName/
I have a link www.example.com/folder/filename.php?cid=1&title=Chekcing
I want to make user friendly url from above link to : www.example.com/folder/1/Checking/
I have an .htaccess code :
**
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\s [NC]
RewriteRule ^ %1 [R,L]
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?cid=([^&\s]+)\&title=([^&\s]+) [NC]
RewriteRule ^ %1/%2/%3? [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+?)/([^/]+)/?$ $1.php?cid=$2&title=$3[QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*?)/?$ $1.php [L]
**
But it only returns filename without .php : **www.example.com/folder/filename/1/Checking**
I want to remove filename too, from the url...
Thanks in Advance...
Try this :
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteEngine On
RewriteBase /foldername/
#Remove .php extensions from php files :
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
#__________
#Redirect urls with query strings to cleaner version :
RewriteCond %{THE_REQUEST} /filename\.php\?cid=([^&]+)&title=([^\s]+) [NC]
RewriteRule ^ %1/%2? [NC,R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ foldername/filename.php?cid=$1&title=$2 [QSA,L,NC]
#___________
My link is
search?c=category&s=product
I have already removed the .php with htaccess, everything is working apart from when a user searches, the first page will show the link above, I want to show the link as below:
search/category/product
I tried the below code:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^product/(.*)/(.*)/(.*) product.php?id=$1&c=$2&name=$3
RewriteRule ^search/(.*)/(.*)/(.*) search.php?c=$1&s=$2&page=$3
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{THE_REQUEST} \ /+search\?c=([^&\ ]+)&s=([^&]+)
RewriteRule ^ /search/%1/%2? [L,R]
What am I missing in the above?
Have rules this way:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /site/
RewriteCond %{THE_REQUEST} /search\?c=([^&\s]+)&s=([^&\s]+) [NC]
RewriteRule ^ search/%1/%2? [L,R]
RewriteRule ^product/([^/]+)/([^/]+)/([^/]+)/?$ product.php?id=$1&c=$2&name=$3 [L,QSA]
RewriteRule ^search/([^/]+)/([^/]+)/([^/]+)/?$ search.php?c=$1&s=$2&page=$3 [L,QSA]
RewriteRule ^search/([^/]+)/([^/]+)/?$ search.php?c=$1&s=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
I'm trying to replace question marks and equal signs so that I can use URLs like the following:
http://www.mydomain.com/categories/id/23/name/category-name
The above url would be internally redirected to
http://www.mydomain.com/categories?id=23&name=category-name
I am using this .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
# external redirect from /view.php?id=1 to /view/id/1
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^.]+)\.php\?([^=]+)=([^\s&]+) [NC]
RewriteRule ^ /%1/%2/%3? [L,R=301]
# internal forward from /view/id/1 to /view.php?id=1
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?$2=$3 [L,QSA]
but it's not working as expected. When I go to http://www.mydomain.com/categories/id/23/name/category-name I get a 500 internal server error. Why? What am I doing wrong?
updated code
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?(.*)$ $1/$2=$3&$4 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/([^/]+)$ $1.php?$2 [L,QSA]
This rule should take care of recursively replacing each / with = and finally making it a nice Query String:
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?(.*)$ $1/$2=$3&$4 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/([^/]+)$ $1.php?$2 [L,QSA]
You need to comment out or delete this rule:
# RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?$2=$3 [L,QSA]
UPDATE: You .htaccess need re-ordering and little bit of tweaking. This should work:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /mydomain/
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?(.*)$ $1/$2=$3&$4 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/]+)/(.+?)&?$ $1.php?$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]