So my staff.php?action=[action] file is rewritten to /staff/[action] where action can take 3 values new/edit/delete.
I want to display success or failure messages on submission. I tried to redirect to /staff/new&error=[error] or /staff/new?error=[error].
However, the var_dump($_GET) only returns the action's query string.
The only solution I found is /staff?action=[action]&error=[error], but I don't like it. Is there any way of rewriting my rules?
I don't want /staff/new/error/[error]
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
RewriteRule ^staff/(.+)$ /staff.php?action=$1 [L]
You can use QSA flag to add query string to your forwarded page
RewriteRule ^staff/(new|edit|delete)$ /staff.php?action=$1 [L,QSA]
Related
When I am trying to give url as https://example.com/dir/file/file then the request is getting into loop and 500 error comes while it should give file does not exists. I am using LAMP Stack. I am hiding .php in my .htaccess
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
You are getting a rewrite-loop (500 error) because the filename you are checking, ie. %{REQUEST_FILENAME}.php isn't necessarily the same as the file you are rewriting to, ie. $1.php.
If you request /dir/file/file then the REQUEST_FILENAME server variable is <document-root>/dir/file (no path-info), whereas the captured backreference $1 is /dir/file/file.
Try the following instead:
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
A request for /dir/file/file will now fail with a 404, since it is checking that /dir/file/file.php exists.
You don't really need to check that the request does not map to a directory before checking that it does map to a file (twice the work), unless you also have directories of the same name and you need the directory to take priority (unlikely).
See also my answer to the following ServerFault question that goes into more detail:
https://serverfault.com/questions/989333/using-apache-rewrite-rules-in-htaccess-to-remove-html-causing-a-500-error
I am not familiar with the rewrite rules..
I am trying to get
export.php?type=pdf
to be able to be
export.php/pdf
I already have rewrite rules to remove .php from files..
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
so really it would be.
export?type=pdf
to
export/pdf
The issue I'm having is how do i go about doing this for JUST the export.php file ? I know its using Reg-ex for the rules, but i don't know enough to make a new rule, i have tried to Google it and find an answer on how to do it, but the issue is I'm not sure what this is called other than ".htaccess rewrite rule"
Try below rule after all your rules,
# incoming url is neither a file nor a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/(.+)/?$ $1.php?type=$2 [L]
I'm looking for a way in which I could redirect https://example.com/foo.php to https://example.com/foo and also https://example.com/foo/ to https://example.com/foo.
There are copious solutions to this posted on Stack Overflow, but none of them transpire to address an instance where the user tries to access a file such as https://example.com/foo/bar/baz.php.
Could anyone kindly propose something to solve this with .htaccess?
The code I have currently is:
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+).php
RewriteRule ^ %1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)/\s
RewriteRule ^ %1 [R=301,L]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
Try these rules:
RewriteEngine On
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.+?)\.php[\s?] [NC]
RewriteRule ^ %1 [R=302,L,NE]
## Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [NE,R=302,L]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Remember to clear your browser cache before testing.
I have the below url that I want to prettify. Any help appreciated.
From;
http://www.example.co.uk/search-menu?r_id=1&name=testname
To
http://www.abklab.co.uk/search-menu/1/testname
I have tried below but getting Internal Server Error
# external redirect from actual URL to pretty one (WEB)
RewriteCond %{THE_REQUEST} \s/+search-menu(?:\.php)?\?r_id=([^\s&]+)&name=([^\s&]+) [NC]
RewriteRule ^ /search-menu/%1/%2? [R=302,L,NE]
# internal forward from pretty URL to actual one (WEB)
RewriteRule ^search-menu/([\w-]+)/([\w-]+)/?$ search-menu.php?r_id=$1&name=%2 [L,QSA,NC]
FYI: Below rule removes .php extension site wide;
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
Try this in your htaccess :
RewriteEngine on
RewriteCond %{THE_REQUEST} /search-menu\?r_id=([^&]+)&name=([^\s]+) [NC]
RewriteRule ^ %1/%2? [NC,L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^search-menu/([^/]+)/([^/]+)/?$ search-menu.php?r_id=$1&name=$2 [NC,L]
Found something for you - the wizard will help you make your URL perfect
http://www.generateit.net/mod-rewrite/index.php
Add this line to your .htaccess file. It will take the variables and make is nice and pretty:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)\.html$ /search-menu?r_id=$1&name=$2 [L]
Keep in mind this is basic and you will need to modify it to your addresses but it is just to give you the idea of what you need to do.
This has been asked a thousand times, but not exactly how I want it. I have tried combining different solutions, but my .htaccess doesn't seem to do what it is supposed to.
# Not sure what this does?
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
# Redirect index in any directory to root of that directory
RewriteCond %{THE_REQUEST} ^[A-Z](3,9)\ /([^/]+/)*index\.[^\ ]*\ HTTP/
RewriteRule ^(([^/]+/)*)index(\.[a-z0-9]+)?$ http://www.example.com/$1? [R=301,L]
Now my pages correctly change from domain.com/page1.php to domain.com/page1, however something goes wrong with domain.com/index.php.
I am testing this locally and when going to localhost/project everything works fine (the index.php opens, but you don't see that in the url) but when you explicitly navigate to localhost/project/index.php you are returned to the very root, i.e. localhost (which hen returns to http://localhost/xampp/splash.php). Of course, this is not what I want. I want localhost/project/index.php to return to `localhost/project/ยด.
An additional question, though: how do rewrite rules influence search engines. Will the pages (ie contact.php, about-us.php and so on) still be indexed?
Extra +1 and kudos for he or she who gives a detailed breakdown of what each line/rule in the htaccess in their answer does. I am still learning .htaccess, so every detail is important and relevant to me!
Lot of comments in this code in question seem to be mine :)
Anyway you can use this code to fix your issue:
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /project/
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} \s/+project/+(.*?/)?(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ %1%2 [R=302,L,NE]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/project/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
Update: For using in DocumentRoot:
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ %1%2 [R=302,L,NE]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]