htaccess rewrite rules, or a PHP issue? - php

I am writing a small web app mainly used just by myself so I'm not interested in fancy frameworks and page templating etc.
I need to be able to rewrite these urls:
/?page=parks
/?page=park&id=1
into
/parks
/park/1
Now, I have got very close with the following rules:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]*)$ /?page=$1 [L]
RewriteRule ^([^/]+)/([^/]+)$ /?page=$1&id=$2 [L]
and this works for most of the pages I have, but if I do the URL /settings it breaks. If I echo $_GET['page'] I get "inc". I have no idea why. Is this a PHP issue, or are my rules wrong?

You don't need RewriteCond %{REQUEST_FILENAME}\.php -f condition for this.
You can use:
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/?$ /?page=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ /?page=$1&id=$2 [L,QSA]

Related

RewriteCond being met in testing but not in practice

I have the following .htaccess:
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [C]
RewriteCond %{REQUEST_FILENAME} ^(.*).php$
RewriteRule ^(.*)$ /index.php [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
I want it to firstly reroute everything to HTTPS.
Then I want to check if the user is accessing a PHP file, such as domain.com/pages/page/a.php and route that to /index.php.
Then lastly I want everything thats not a file or a directory to route to index.php such as domain.com/my-fun-page
I think the first and the last blocks are correct, but for some reason when I run a test on htaccess tester It works successfully. However when I run it on my own server it still lets me access domain.com/pages/page/a.php.
How can I find out why, or what am I doing wrong?
Try with below for php part,
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^ /index.php [NC,L,QSA]
You can combine your 2nd a 3rd rule into a single rule like this:
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,LE]
RewriteCond %{REQUEST_FILENAME}\.php -f [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Even RewriteCond %{REQUEST_FILENAME}\.php -f [OR] may not be necessary as !-f and !-d will already cover this case.

htaccess rule to remove .php confilicting with another rewrite

So I had a rule the removed the necessity of having .php after my admin page, 'admin.php':
# Don't require .php extension to load php page
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /$1.php [L]
The rule worked fine untill I needed to add the below rules;
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /?action=viewCategoryName&categoryName=$1&page_identifier=$2 [L,QSA,B]
and, the one effecting the first rule;
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)/?$ /?action=viewHomepageName&page_identifier=$1 [L,QSA,B]
Im wondering if how I've made the CMS and its query strings is lending to this being impossible to rectify. Could do a redirect from admin.php to admin/ but if possible I would like the usability of the first rule carried forward.
Cheers!
Adam
Organize your rules like this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
RewriteRule ^([^/]+)/([^/]+)/?$ /?action=viewCategoryName&categoryName=$1&page_identifier=$2 [L,QSA,B]
RewriteRule ^([^/]+)/?$ /?action=viewHomepageName&page_identifier=$1 [L,QSA,B]

Replace GET variables with slashes .htaccess

I am trying to use .htaccess to make my url's "prettier".
Changing domain.com/page/val1
To domain.com/page.php?var1=val1
If possible, I would like it to work with 2 variables as well:
Changing domain.com/page/val1/val2
To domain.com/page.php?var1=val1&var2=val2
This is my current .htaccess file that works to remove the .php extension:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
I've found other topics with solutions similar to adding something like this:
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /$1/$2?var1=$3&var2=$4 [L,QSA,NC]
I've tried many ways to modify it and it never seems to work.
Any help would be appreciated. Thanks.
Try this code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)/?$ $1.php?var1=$2 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ $1.php?var1=$2&var2=$3 [L,QSA]

Mod_rewrite for url with?

I can't find my answer on the other questions here in StackOverflow..
mod_rewrites generator are failing so I don't know how to do that..
I have that urls: http://example.com/?index&category=football
football are dynamic generator by php, so there is many categories.
So I want my URL like this: http://example.com/football
How to do that with mod_rewrite?
Thank you so much!
RewriteEngine on
RewriteRule ^/([a-zA-Z0-9]+)$ /index.php?category=$1
#RewriteRule ^/([a-zA-Z0-9]+)/$ /index.php?category=$1
Try:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+\?index&category=([^&\ ]+)
RewriteRule ^ /%1? [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /?index&category=$1 [L,QSA]
This should makes URL http://example.com/football works as if http://example.com/?index&category=football was used:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^/?([^/$]*) /?index&category=$1 [L]
Lines 2 to 5 (from RewriteCond %{REQUEST_FILENAME} -s to RewriteRule ^.*$ - [NC,L]) are optional: they makes any existing file named as a category name (eg. football) be served directly (not using /?index&category=...).

Is it possible to combine these two .htaccess files?

I have to completely replace an existing website. The current site is completely spagetti code with some rewrite rules to mimic friendly urls.
There are some mission critical issues that can't be resolved with the current architecture and database structure, so for a time both code bases need to live side by side.
This is the current .htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ $1.php?q=$2
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ $1.php?q=$2&r=$3
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ $1.php?q=$2&r=$3&s=$4
I'm replacing it with a zend framework site, but it routes everything through the index.php file for the routing
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule .* - [L]
RewriteRule .* index.php
Does anyone have any idea how I can combine the two until the original can be completely replaced?
If you can map your old URLs to Zend Url someway, then it would be rather easy. Let's say you had
http://foo.bar.com/something.php?q=arg1&r=arg2
then if you have this functionality working in Zend code, then you must also have valid URL there, like
http://foo.bar.com/something/q/arg1/r/arg2
If so, all you need is to rewrite that old URLs to Zend URL and instead of doing internal redirection, do HTTP redirection, i.e. (out of my head, not tested):
RewriteCond %{QUERY_STRING} ^q=(.*)&r=(.*)^
RewriteRule ^something\.php$ /something/q/%1/r/%2? [R=301,L]
Mind the trailing "?" which tells mod_rewrite NOT to attach original query string to rewritten one (otherwise you would end with /something/q/%1/r/%2?q=X&r=y. Note we do regular 301 HTTP redirection here.
They're not going to merge very easily, it's a matter of which one you want to have precedence. You could add a few more conditions to make sure everything doesn't get routed as a $1.php file, then add the zend rules to the end.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{REQUEST_URI} ^([^/]+)/
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^([^/]+)/([^/]+)/$ $1.php?q=$2
RewriteCond %{REQUEST_URI} ^([^/]+)/
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ $1.php?q=$2&r=$3
RewriteCond %{REQUEST_URI} ^([^/]+)/
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ $1.php?q=$2&r=$3&s=$4
# other rules
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule .* - [L]
RewriteRule .* index.php

Categories