So, I have this code in .htaccess:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Every file loses its extension (/index.php -> /index etc) and that works perfectly fine. But later on, I started working with admin panel and I'm using some GET parameters there. For example, problematic URL look like:
example.com/admin?cat=1
As far as I know, RewriteRule gets only string after RewriteBase and is not catching GET parameters, right? So why when I try to go to this URL it rewrites it to this?
http://example.com/C:/OpenServer/domains/example.com/admin/1/
There is also this line in .htaccess(but doesn't look like there is problem with it):
RewriteRule ^admin/(.*)$ admin.php?cat=$1
As answered here, you have to add [QSA] at the end of your RewriteRule line:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [QSA]
Google search query that lead me to that answer: htaccess pass parameters
Related
I have a URL:
domain.com/abc/hotel_detail.php?id=2
And I want to do a URL re-write to make it look like this :
domain.com/abc/hotel_detail/2
My current .htaccess looks like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{QUERY_STRING} ^id=20$
RewriteRule ^abc/hotel_detail/([0-9]+)$ ^abc/hotel_detail.php?id=$1 [L]
The first part contains code for removing the extension(.php) from pages.
When I try to open this link domain.com/abc/hotel_detail/2 , it gives me object not found error.
Can Someone please tell me whats wrong with the code?
Remove this line:
RewriteCond %{QUERY_STRING} ^id=20$
Changing your file to:
RewriteEngine On
RewriteRule ^abc/hotel_detail/([0-9]+)$ ^abc/hotel_detail.php?id=$1 [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,R]
should work. I removed RewriteCond %{QUERY_STRING} ^id=20
Your first rewritecond and rule was overwriting the second one, switching them around should fix it.
I think this should work
RewriteRule ^abc/hotel_detail/([0-9]*)$ abc/hotel_detail.php?id=$1 [L]
([0-9]*) miltiply sign.
and the ^ for the real link
I am trying to find a better solution to solve the issue. I am not using any framework like silex, Yii, etc. Thus I do not have the general routing handling mechanism provided by these frameworks. My URL patterns are like
routing.php
routing.php?action=create
routing.php?action=list&id=1
and the resulting URL I want to have are
/routing
/routing/create
/routing/list/1
I have some very basic understanding of .htaccess, below is my current foundings
RewriteEngine On
RewriteBase /name/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
These codes work well to put any .php to /something. However, I am stuck to continue to generate a general solution for getting /routing/create and /routing/list/1 . I appreciate for any helps.
Have your .htaccess like this:
RewriteEngine On
RewriteBase /name/
RewriteCond %{DOCUMENT_ROOT}/name/$1.php -f
RewriteRule ^([^/]+)/([^/]+)/?$ $.php1?action=$2 [L,NC,QSA]
RewriteCond %{DOCUMENT_ROOT}/name/$1.php -f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ $1.php?action=$2&id=$3 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
I am attempting to create an seo friendly url system.
Sample:
/category would get information from cat.php?cat=category
My issue is that I want to keep that url and be able to do something like the following
/category?page=2&sub=1
or
/category/?page=2&sub=1
I was able to get the folders to redirect to the cat.php file but I cant seem to figure out the second half.
#Create redirect for all nonexistant folders to the category file
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)$
RewriteRule ^(.*)/? cat.php?cat=$1 [L]
If I am understanding you this should be what you need.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ cat.php?cat=$1 [QSA,L]
So if you were to use a URL like this
http://www.yoursite.com/category?page=2&sub=1
The RewriteRulewill direct it to cat.php?cat=category&page=2&sub=1
You're lacking the QSA flag at the very least. Without QSA, the existing query string is simply replaced by the new query string.
at the moment I've got the following code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
What this code does is allow users to go to /pagename instead of /pagename.php
Is there a way to make it so this only works for a specific page because at the moment this code makes this sitewide?
add a new RewriteCond after the two rewriteCond and check if the request is for the specific page.
RewriteCond %{REQUEST_URI} ^/(myPage)/
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
you can also use the RewriteBase directive.
Example
How does RewriteBase work in .htaccess
This is my htaccess file at the moment
RewriteEngine On
# Only redirect if file dosen't exist.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin/(.*) /admin.php
RewriteRule ^([^/]) /index.php [L]
I don't have any idea why this doesn't work. I think I've finally grasped mod_rewrite and then it just does something completely unexpected like this.
Basically if the URL is domain.com/admin/something then I want it to redirect to domain.com/admin.php (including if its just /admin). However if its ANYTHING else I want it to redirect to index.php.
Any idea how to achieve this?
Thank you.
RewriteEngine On
# No redirect, if file or directory exists
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^admin/(.*) /admin.php [L]
RewriteRule ^(.*)$ /index.php [L]
Didnt test it. The interesting part is the L-Flag after the admin-rule, because it prevents the next rule from matching.
I changed the RewriteCond-Statements, because they only apply to the one next RewriteRule and (in your case) doesnt affect the rule to index.php.