URL rewriting: conflicting rewrite rules - apache

I'm trying to do three things with my URL rewriting:
remove .php file extensions
have my post URLs look like this: mydomain.co.uk/resources/example-post The default is mydomain.co.uk/resources/post.php?s=example-post
have my category archive URLS look like this: mydomain.co.uk/resources/category/example-category The default is mydomain.co.uk/resources/archive.php?cat=example-category
This is what I have in my .htaccess file at the moment:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [L,QSA]
RewriteRule ^resources/([a-zA-Z0-9-/]+)$ /resources/post.php?s=$1 [L]
RewriteRule ^resources/category/([a-zA-Z0-9-/]+)/page/([0-9]+)/{0,1}$ /resources/archive.php?cat=$1&page=$2 [L]
RewriteRule ^resources/category/([a-zA-Z0-9-/]+)$ /resources/archive.php?cat=$1 [L]
It's doing the first two things. But not the third.
If I remove
RewriteRule ^([^\.]+)$ $1.php [L,QSA]
Then it does the third things, but not the first two.
Any ideas what's going? How do I get it do all three things at once?

Found a solution.
Just needed to swap the order around, I now have this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [L,QSA]
RewriteRule ^resources/category/([a-zA-Z0-9-/]+)/page/([0-9]+)/{0,1}$ /resources/archive.php?cat=$1&page=$2 [L]
RewriteRule ^resources/category/([a-zA-Z0-9-/]+)$ /resources/archive.php?cat=$1 [L]
RewriteRule ^resources/([a-zA-Z0-9-/]+)$ /resources/post.php?s=$1 [L]
It's working fine.

Related

.htaccess redirection rules with parameter rewrite

I have the following .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Which redirects every server/path request to server/path.php file.
I want to add one more specific rule, to redirect everything of the form /item/id to /item.php?var=id
Can I make it work together?
Appreciate the help.
Try with below rule,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)$ $1.php?var=$2 [NC,L]

.htaccess rewriterule for username like facebook

I am utilising following code for my current application
RewriteBase /
#for profile display
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^user/([^/]+)/([^/]+)/([^/]+)/?$ /profile.php?username=$1&option=$2&source=$3 [QSA]
RewriteRule ^user/([^/]+)/([^/]+)/?$ /profile.php?username=$1&option=$2 [QSA]
RewriteRule ^user/([^/]+)/?$ /profile.php?username=$1 [QSA]
#for hiding.php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
What this will do is, it will treat URL www.domain.com/user/johndoe/photos/photoid-123 as www.domain.com/profile.php?username=johndoe&option=photos&source=photoid-123
Now, what I want to do is to remove /user/ part from the url and keep it like www.domain.com/johndoe/photos/photoid-123
I have tried modifying rewrite rule like RewriteRule ^([^/]+)/.... OR RewriteRule ^/([^/]+)/.... OR RewriteRule ([^/]+)/...., but it is giving me 500 Error
Any suggestions?
Have it this way:
DirectoryIndex index.html index.php
RewriteEngine On
RewriteBase /
#ignore files and directories from rewrites
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ profile.php?username=$1&option=$2&source=$3 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/?$ profile.php?username=$1&option=$2 [QSA,L]
RewriteRule ^([^/]+)/?$ profile.php?username=$1 [QSA,L]
#for hiding.php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !\.php$ %{REQUEST_FILENAME}.php [QSA,L]

multiple RewriteRules in htaccess file, the first one over writes the second one?

I am trying to run multiple RewriteRules in my htaccess file.
however, the first RewriteRule, overwrites the second RewriteRule for some reason!
this is what i have in my htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^([a-zA-Z0-9-/]+).html$ items.php?itemsurl=$1 [L]
RewriteRule ^([a-zA-Z0-9-/]+).html/$ items.php?itemsurl=$1 [L]
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^([a-zA-Z0-9-/]+).html$ blog.php?blogurl=$1 [L]
RewriteRule ^([a-zA-Z0-9-/]+).html/$ blog.php?blogurl=$1 [L]
the first RewriteRule works fine but if I click on the links for blog.php?blogurl= it will simply take me to the items.php!
I could put the RewriteRules for the blog.php at the top of the htaccess file and put the RewriteRules for items.php bellow it and it will make the blog.php rewriterule work for blog.php but it will make the rewriterules for the items.php stop working and everything will point to the blog.php page!
So basically, only the first RewriteRule in the htaccess file works and it will overwrite the second one somehow.
could someone advise on this please?
Replace your code with this one:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-/]+)\.html\/?$ items.php?itemsurl=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-/]+)\.html\/?$ blog.php?blogurl=$1 [L]
RewriteCond affects only to the first RewriteRule.
The apache variables should be in these brakes: {}, not these ()
And one more thing - to check . you should escape it so: \.
As discussed in comments, you can use following rules:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^items/([\w-]+)\.html\/?$ items.php?itemsurl=$1 [L,QSA,NC]
RewriteRule ^blog/([\w-]+)\.html\/?$ blog.php?itemsurl=$1 [L,QSA,NC]

htaccess remove .php extension from the following

I have this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ ./index.php?path=$1 [L,NC,QSA]
I need the .php extension removed from the browser address
This is meant to answer your comment below the question:
RewriteEngine on
RewriteRule ^(.*)\.php(.*)$ $1$2 [QSA,L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ ./index.php?path=$1 [L,NC,QSA]
However I do not really see a point in this. Where should the ".php" extension in those urls come from? I mean users won't write down a url to query your system themselves. Instead they click on a link and that link must be something you sent out before. So obviously your approach should be to send out links that do not contain that file name extension in the first place!
You can use these 2 rules for hiding .php extension:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.*)$ $1.php [NC,L]

RewriteRule all except other rewrite already declared

I have the following entries handled as fixed files using .htaccess (for example)
RewriteEngine On
RewriteRule ^redir$ redir_base64.php
RewriteRule ^confirm$ confirm.php
RewriteRule ^migrate$ migrate.php
RewriteRule ^import_conn$ import_conn.php
RewriteRule ^callback_ot$ callback_ot.php
RewriteRule ^callback_yh$ callback_yh.php
RewriteRule ^callback_gp$ callback_gp.php
RewriteRule ^callback_lk$ callback_lk.php
RewriteRule ^results/map$ index.php
RewriteRule ^results$ index.php
RewriteRule ^robots\.txt$ robots.php
RewriteRule ^sitemap\.xml$ sitemap.php
RewriteRule ^removeml$ removeml.php
RewriteRule ^rss$ rss_gen.php
RewriteRule ^cpt\.jpg$ c/captcha/captcha.php
RewriteRule ^cptfrm\.jpg$ c/captcha/captcha.php?width=120&height=60&characters=4
RewriteRule ^cptfrm160\.jpg$ c/captcha/captcha.php?width=100&height=60&characters=3
RewriteRule ^login/lk$ login_conn.php?op=lk
RewriteRule ^t$ c/thumb/phpThumb.php
RewriteRule ^spce$ spce.php
RewriteRule ^orplkd$ order_publisher.php
RewriteRule ^ord_ren$ order_reactivate.php
RewriteRule ^logout$ logout.php
This entry works fine! But I need to access /000/000 and I see that an conflict with lines indicates below.
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\d\w\-\.]+)/([\d]+)$ index.php [QSA,L]
RewriteRule ^([\d\w\-\.]+)$ index.php [QSA,L]
I try to access to my site using that lines but I cannot to recognize only the last lines because use ALL access on the site.
I hope so understand my help.
Thanks!
You need to repeat the 2 conditions. Rewrite conditions only apply to the immediately following condition.
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\d\w\-\.]+)/([\d]+)$ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\d\w\-\.]+)$ index.php [QSA,L]