.htaccess mod_rewrite on multiple PHP files issue - php

I'm trying to beautify URL's for SEO from http://example.net/dashboard.php?action=news to http://example.net/news
And it's working with this code
RewriteEngine On
RewriteCond %{THE_REQUEST} \/+dashboard\.php\?([^\&]+) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ dashboard.php?action=$1 [L,QSA]
But I want to edit more PHP files, for example, http://example.net/user.php?action=profile to http://example.net/profile
If I use this code again in the same .htaccess file
RewriteEngine On
RewriteCond %{THE_REQUEST} \/+user\.php\?([^\&]+) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ user.php?action=$1 [L,QSA]
It won't work, only dashboard.php one is working properly.
Can you please help me with this?
Thank you in advance.

Your current Rewriterules are to general.
[^/]+
matches everything which has no "/", else its matching everything.
So basicly your first rule
RewriteRule ^([^/]+)/?$ dashboard.php?action=$1 [L,QSA]
matches everytime. profile/ will always redirected to dashboard.php?acton=profile.
Your .htaccess should look like:
RewriteEngine On
RewriteCond %{THE_REQUEST} \/+dashboard\.php\?([^\&]+) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(news)/?$ dashboard.php?action=$1 [L,QSA]
RewriteCond %{THE_REQUEST} \/+user\.php\?([^\&]+) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(profile)/?$ user.php?action=$1 [L,QSA]
As you can see i switched
[^/]+
with more explicit regex.

Related

Ignore a particular folder in htaccess

I am facing issues regarding my .htaccess. The .htaccess works, but it causes a problem with the admin folder. Would it be possible to exclude that specific folder from this .htaccess? Here is my .htaccess. It is outside the admin folder.
Options -MultiViews
ErrorDocument 404 http://localhost/blo-prt/err.php
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L,QSA]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blo/([^/\.]+)?$ blo-det.php?postlist=$1 [L,QSA]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category/blo/([^/\.]+)?$ blo-prior.php?bloglist=$1 [L,QSA]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^tep/([^/\.]+)?$ temp-det.php?telist=$1 [L,QSA]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category/tep/([^/\.]+)?$ temp-of-prior.php?tlist=$1 [L,QSA]
You should add this to your RewriteConds:
# if request is not for the /admin/
RewriteCond %{REQUEST_URI} !^/admin/ [NC]
Personally, I use this code to remove the .php extension:
# enable url rewriting
RewriteEngine On
# remove .php
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
Also, you should only type RewriteEngineOn just once at the top of the file.

Mod_Rewrite with parameters - Dynamic Website

I want to change my URLS.
At the moment they look like this:
http://website.net/?page=news
http://website.net/?page=information&skin=217 (shows a mario skin with informations)
I want them to look like this:
http://website.net/news/
http://website.net/mario/
What I have tried so far is following:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^page=information&skin=217$
RewriteCond %{REQUEST_URI} ^\/$
RewriteRule .* http://website.net/Mario/? [R=301,L]
^ This Code works for changing the URL-Addresse, but I get a 404 Error: Page not found. Seems like this isnt working too well.
What did I miss and what do I have to change?
This also has to work with given parameters so: http://website.net/Mario/ should still know the parameters page=information, skin=217
Thanks
You can use this code in site root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+\?page=information&skin=217\s [NC]
RewriteRule ^ /mario/? [R=301,L]
RewriteCond %{THE_REQUEST} \s/+\?page=([^\s&]+)\s [NC]
RewriteRule ^ /%1/? [R=301,L,NE]
RewriteRule ^mario/?$ /?page=information&skin=217 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ /?page=$1 [L,QSA]
`RewriteCond %{THE_REQUEST} \s/+\?page=information&skin=([^\s&]+)\s [NC]
RewriteRule ^ /%1/? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ /?page=information&skin=$1 [L,QSA]
RewriteCond %{THE_REQUEST} \s/+\?page=information&resourcepack=([^\s&]+)\s [NC]
RewriteRule ^ /%1/? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ /?page=information&resourcepack=$1 [L,QSA]`
I have this now, works fine with all skins, but doesnt work for resourcepacks. Why?
Also tried to change %1 and $1 in the second blog to %2 and $2.

Friendly URL with two properties?

I am trying to rewrite an ugly URL that's using two properties. For example, I want example.com/stories/?url=how-are-you?num=5 to be example.com/stories/how-are-you/5 I have rewritten the first property, but I don't know how to rewrite the num property. Here's my .htaccess file:
RewriteEngine On
RewriteBase /stories/
RewriteCond %{THE_REQUEST} /\?url=([^&\s]+) [NC]
RewriteRule ^ %1? [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?url=$1 [L,QSA]
Try :
RewriteEngine On
RewriteBase /stories/
RewriteCond %{THE_REQUEST} /\?url=([^&\s]+)&num=([0-9]+) [NC]
RewriteRule ^ %1/%2? [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?url=$1&num=$2 [L,QSA]

issue in url rewrite in .htaccess

i am trying to force non-www to www and its working as aspected:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
now if i visit www.example.com/registration it works great.. but when i am accessing the same url from an link it works but adds index.php?/ in the url like this:
www.example.com/index.php?/registration
i am new to .htaccess programming.. i can understand i need ot modify the below lines.. but i dont know how to modify in such a way so that i does not add index.php?/ in the url...
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
any help or suggestion would be great help.. thanks in advance
Swap the order of your rules:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

htaccess rewrite rule to index.php?p=pagename

I am using this htaccess code to create a rewrite rule
RewriteCond %{HTTP_HOST} ^my\.domain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f
RewriteRule ^(.*)$ index.php?p=$1.php [L,QSA]
i want to rewrite pages like
http://my.domain.net/index.php?p=tickets/openticket to look like http://my.domain.net/tickets/openticket
but its just showing index.php without the ?p=...
Try this code as your first rules:
RewriteCond %{THE_REQUEST} /index\.php\?p=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?p=$1 [L,QSA]
RewriteRule ^(.*)$ index.php?p=$1 [L,QSA]
try this.

Categories