Hello there i have a little problem.
When using these lines of .htaccess
RewriteEngine on
Options +FollowSymLinks
# clean up file extensions .php only
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
#make get request clean
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^cms/tests/([0-9]+) cms/tests/index.php?survey=$1 [NC,L]
I transform the url
cms/tests/index.php?survey=65 to -> cms/tests/65
This works!
Now i want to have an edit mode like this:
cms/tests/65/edit or cms/tests/65/view
This is the rewrite rule I have come up with:
RewriteEngine on
Options +FollowSymLinks
# clean up file extensions .php only
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
#make get request clean
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^cms/tests/([0-9]+)/([a-zA-Z0-9_-]+) cms/tests/index.php?survey=$1&mode=$2 [NC,L]
When writing the full url like this cms/tests/index.php?survey=65&mode=edit it completely works
But it with the "clean" url it goes to my 404 page and also shows a 302 redirect in the network tab.
Is there a something that I am doing wrong or over seeing?
#make get request clean
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^cms/tests/([0-9]+)/([a-zA-Z0-9_-]+) cms/tests/index.php?survey=$1&mode=$2 [NC,L]
The second condition is never successful so the rule is not processed.
The second condition (that supposedly checks that the request + .php exists) is not what you should be doing here. If anything, you would need to check that the request does not map to a file. For example:
RewriteCond %{REQUEST_FILENAME} !-f
However, if you restrict the regex in your RewriteRule pattern by appending a $ (end-of-string anchor) then you can remove that condition altogether. For example:
#make get request clean
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^cms/tests/([0-9]+)/([a-zA-Z0-9_-]+)$ cms/tests/index.php?survey=$1&mode=$2 [NC,L]
A request that matches the regex as stated could not map to a real file (unless you have extensionless files).
Aside:
# clean up file extensions .php only
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
The second condition in your first code block is also not strictly correct and will fail under certain conditions, because the filesystem check is not necessarily the same as the file you will ultimately rewrite to, which could result in a 404 or 500 (rewrite loop) depending on your file structure. (This code block is surprisingly common, but issues relating to this keep on cropping up.)
This should really be written more like this:
# clean up file extensions .php only
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
Now, the condition %{DOCUMENT_ROOT}/$1.php matches the rewrite substitution $1.php. (Assuming the .htaccess file is in the document root.)
See my answer to the following question on ServerFault with a detailed explanation of this change: https://serverfault.com/questions/989333/using-apache-rewrite-rules-in-htaccess-to-remove-html-causing-a-500-error
Related
I want that if a user types in www.example.com/article-title, it gets the data from www.example.com/index.php?title=article-title. Right now it only works if a user types in www.example.com/article/article-title. I want to remove that article/.
This is what I have right now in my .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC]
RewriteRule ^article/([0-9a-zA-Z]+) index.php?title=$1 [NC,L]
If I remove article/ from the last line in my .htaccess file, it doesn't work at all.
The $1 should be article-title, but if I remove article/, $1 becomes index.
Does anyone know how I can change the .htaccess in order to let users type in www.example.com/article-title?
You just need
RewriteEngine on
RewriteRule ^(.*)$ index.php?title=$1 [NC]
tested here with
input: https://www.example.com/article-title
output: https://www.example.com/index.php?title=article-title
However i suppose this is actually what you really want, because by the looks of things you are not locking for a redirect, but something like:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
And then in the index.php file you have to "manipulate" the url requested
forcing this to work makes my kind of crazy so i hope you can help.
I use Rewrite Rules and .htaccess to make my dynamic URL
example.com/page.php?id=1
look like this
example.com/1
using
RewriteRule ^([A-Za-z0-9-]+)/?$ page.php?id=$1 [NC,L]
, and it works perfectly fine so far.
But i also want to hide the filetype in the URL ( impressum.php to impressum) using
RewriteRule (.*) $1.php [L]
So both Rules are working completely correct as long as i dont use them both at the same time. When i do so, which looks like this (my complete file)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
RewriteRule ^([A-Za-z0-9-]+)/?$ page.php?id=$1 [NC,L]
,i get an Internal Server Error. I tried different versions, for example change the positions and so on, but i allways get this error.
So my question is: how do i get both rules together and working, while the URL ending is still hidden and the example.com/1 works too?
Thank you very much for any answer
You can use the following in your .htaccess file:
RewriteEngine on
# Check if the PHP file exists and route accordingly
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
# If not, pass the request to page.php if it contains A-Za-z0-9-
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z0-9-]+)/?$ page.php?id=$1 [NC,L]
You need two separate rules. Rewrite conditions will only get applied to the immediately following rule and with your php extension rule, you must check that the php file exists before adding the php to the end:
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9-]+)/?$ page.php?id=$1 [NC,L]
I have the following .htaccess:
RewriteEngine On
RewriteBase /shared/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /shared.php [L]
But I would also like to remove .php extension with the following:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
I have tried a dozen combinations based on answers elsewhere not just on StackOverflow but still cannot get it right, I either render pages not in shared directory unable to open with 500/404 errors or 500 error when I go to /shared.
After further investigation and trial when I add the rules to remove the .php extension it messes up the first rule to route anything under the path of /shared/ to shared.php the path /shared/username are not real locations but the script insures that the correct information is presented. It would be handy to ignore the second rule if the URL has /shared/ in the path? Is that possible? I am not rewriting everything to the /shared/ directory - only when the path reads /shared/username do I want that rule to kick in, everything else should be rewritten to the / base directory.
Keep your rules like this:
RewriteEngine On
RewriteBase /shared/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /shared.php [L]
I know this question has been asked before but does anyone know of a good way to hide .html extensions. I've tried many of codes & many of the answers from the https://stackoverflow.com/ but am not seeing the result. Thats y I ask u again
I've a static website and I wanted to remove the extension to clean my urls. I was working with static html files.
Prior to removing the extension, the url would read website.com/about.html.
With the extension removed, it would look like website.com/about. Hope that was clear.
I've a .htaccess file and I tried many of the codes but it dosen't work. Here are some codes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
RewriteCond %{THE_REQUEST} \ /(.+/)?index(\.html)?(\?.*)?\ [NC]
RewriteRule ^(.+/)?index(\.html)?$ /%1 [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.html$ /$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://%{HTTP_HOST}/ [R=301,L]
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [NC,L]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteBase /
RewriteEngine on
RewriteRule ^(.*)\.htm$ $1.html [nc]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [L]
however I am not seeing any results...:(
You have your rule reversed (the first one would work otherwise):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [L]
Your original rule was RewriteRule ^(.*)\.html$ $1 [L] which translates to "If the requested resource name ends with .html then issue a redirect to the browser telling it to ask us if we have anything at the same name without the HTML". This results in a 404 (since Apache doesn't have anything to serve for the .html'less resource).
By switching the .html to the destination RewriteRule ^(.*)$ $1.html [L] we change the rule to say "If the requested resource name is not a file or directory on disk then try to re-route the request (internally, don't tell the browser) as if it ended in .html".
Note that you do not need complex (by nature) RewriteRules for such task.
The mod_negotiation apache module, often enabled by default, provides such behavior with Multivews option.
If the server receives a request for /some/dir/foo and /some/dir/foo does not exist, then the server reads the directory looking for all files named foo.*, and effectively fakes up a type map which names all those files, assigning them the same media types and content-encodings it would have if the client had asked for one of them by name. It then chooses the best match to the client's requirements, and returns that document.
.i.e requesting for foo/bar will serve foo/bar if it us a directory and will seak for foo/bar.html, foo/bar.txt and such etc if not.
You just need to ensure this option is activated in your current context (a Directory for example)
Options +Multiviews
use this
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [L,QSA]
Look at this post http://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/ I haven't tried it yet but the demonstration seems pretty convincing
I hope you can help. I'm trying to write a .htaccess file to do the following.
redirect to www. address
remove .php from URL
If the file doesn't exist then use filechecker.php?page=filename
#1 I can do with
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#2 I can do with
RewriteCond %{SCRIPT_FILENAME}.php -f
RewriteRule [^/]$ %{REQUEST_URI}.php [QSA,L]
#3 I thought
RewriteCond %{REQUEST_FILENAME}.php !-f
RewriteRule ^([^/]*)$ filechecker.php?page=$1 [QSA,L]
would work, but for some reason it is ignoring the fact that the page does actually exist.
Your solution for 2 will loop, but simple enough to fix, stick something along the following lines in your file for part 2 and 3:
#If the Browser request contains a .php, instruct the browser to remove it.
RewriteCond %{THE_REQUEST} \.php [NC]
RewriteRule ^/?(.*)\.php$ http://%{HTTP_HOST}/$1 [R=301,NC,L]
#If a request is received for a non file-system object, that doesn't have a .php suffix, then store the full path, filename, and URI in a variables with that extention.
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-s
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !^\.php$ [NC]
RewriteRule ([^/]+)$ - [E=testScript:%{SCRIPT_FILENAME}.php,E=testFile:$1.php,E=testURI:%{REQUEST_URI}.php]
#See if the file exists with a .php extention, if it does internally rewrite
RewriteCond %{ENV:testScript} -s [OR]
RewriteCond %{ENV:testScript} -f
RewriteRule .* %{ENV:testURI} [L]
#Else if a ENV:testDile is set, then pass the name to the php script
RewriteCond %{ENV:testFile} !^$
RewriteRule .* /filechecker.php?page=%{ENV:testFile} [L]
FYI: The Apache mod_rewrite documentation is worth a read, if your unclear as to what the above rules do, or if you want something a little more abstract consider the following post.