My current .htaccess file looks like this:
RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/?$ $1.php [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ $1.php?one=$2 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ $1.php?one=$2&two=$3 [L]
DirectoryIndex Home.php
ErrorDocument 404 /404
There's no error here, but as soon as I add the following
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ $1.php?one=$2&two=$3&three=$4 [L]
My webpage gives an Internal Server Error
What am I doing wrong?
Neither of your .htaccess scripts are giving any errors to me or my own website.
I've tested your Regular Expressions on two websites, and give no errors.
http://www.phpliveregex.com/p/aN7
https://www.debuggex.com/r/UBHXW6KJTDmRqewc
Whatever may have caused the error, its not your RewriteRule.
I suggest you start off with a new, clean .htaccess file, testing every time you add a new line. I suggest you add the DirectoryIndex last.
Related
This is super simple but it's driving me crazy! I have a website at http://example.org/ and a subdirectory at http://example.org/ccc/
I want to redirect anything outside of the /ccc/ directory to a different website.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/ccc/?.*
RewriteRule ^(.*)$ https://new-website.com/$1 [L]
But this code doesn't work, it redirects the /ccc/ directory. According to my research and testing with this htaccess tester, it should not redirect because the RewriteCond is checking against /ccc with optional slash and other characters after it.
What is happening? Does this look correct?
Edit: This method from this answer is also not working, the CCC domain is being redirected:
RewriteEngine on
RewriteRule ^ccc index.php [L]
RewriteRule (.*) https://new-website.com/$1 [R=301,L]
PHP 5.4.45, Apache/2.2.31
Assuming ccc/ directory doesn't have a separate .htaccess, you may use this rule:
RewriteEngine on
RewriteCond %{THE_REQUEST} !\s/ccc[/?\s] [NC]
RewriteRule ^ https://new-website.com%{REQUEST_URI} [L,R=301,NE]
THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of other rewrite directives. An example value of this variable is GET /index.php?id=123 HTTP/1.1
It looks like [L] isn't behaving normally and I'm guessing it's the old version of Apache (2.2.31) because these rules worked on a separate website. I found this solution which seemed to work for this case, the third line below:
RewriteEngine on
RewriteRule ^ccc/? index.php [L]
RewriteCond %{ENV:REDIRECT_STATUS} != 200
RewriteRule ^(.*)$ https://new-website.com/$1 [L]
Explanation from that question:
The problem is that once the [L] flag is processed, all the next RewriteRules are indeed ignored, however, the file gets processed AGAIN from the begin, now with the new url.
This magic Condition will not process the catch all if the file was already redirected.
RewriteEngine on
RewriteRule ^home index.php [NC,L]
RewriteRule ^news news.php [NC,L]
Would appreciate any help on how to address this. I have created a .htaccess file for my site in order to simplify its URLs. The entire .htaccess reads as follows (and works as desired):
RewriteEngine on
The only issue I'm facing now is that clicking on it generates a Server Error 500 page instead of the facebook share window.
What can I do to fix this issue
You should use end anchor in your patterns and turn MultiViews off:
Options -MultiViews
RewriteEngine on
RewriteRule ^home/?$ index.php [NC,L]
RewriteRule ^news/?$ news.php [NC,L]
RewriteRule ^news/([0-9a-z]+)/?$ news.php?id=$1 [NC,L,QSA]
Without end anchor ^news pattern will also match news.php and cause infinite looping and eventually 500 (internal server error).
Apparently Bingbot is getting caught in an infinite loop on my site. It downloads pages like http://www.htmlcodetutorial.com/quicklist.html/applets/applets/applets/applets/applets/applets/applets/applets/applets/applets/applets/applets/applets/applets/sounds/forms/linking/frames/document/linking/images/_AREA_onMouseOver.html . Since I set my server to interpret .html as PHP the page is simply a copy of http://www.htmlcodetutorial.com/quicklist.html . How do I stop Bingbot from looking for these bogus copies?
Why is Bingbot looking for those pages to begin with?
I'd like to do something like the last line of the .htaccess file shown below (like at "Redirect to Apache built-in 404 page with mod_rewrite?"), but when I try RewriteRule ^.*\.html\/.*$ - [R=404] the entire site shows a 500 error.
Even when I use the last line below it redirects to http://www.htmlcodetutorial.com/home/htmlcode/public_html/help.html which is not what I wanted.
AddType application/x-httpd-php .php .html
RewriteEngine on
Options +FollowSymlinks
RewriteRule ^help\/.* help.html [L]
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.htmlcodetutorial.com/$1 [R=301,L]
ErrorDocument 404 /404.html
RewriteRule ^.*\.html\/.*$ help.html [R=301]
P.S. I know the site is way out of date.
The problem here is that you either have Multiviews turned on, or apache is interpreting requests like /quicklist.html/blah/blah as a PATH_INFO style request, which will be interpreted as a valid request.
So turn off multiviews by changing your options line to:
Options +FollowSymlinks -Multiviews
Then replace your last rule with:
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^ - [L,R=404]
Change your last rule to this:
RewriteRule ^(.+?\.html)/.+$ - [R=404,L,NC]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
I'm using wamp apache on windows 7. mod_rewrite.so has been enabled in httpd.conf. This code was directly taken from a tutorial found here: http://anantgarg.com/2009/03/13/write-your-own-php-mvc-framework-part-1/
It's purpose is to redirect calls to the ./public folder where the actual logic lies.
I get an internal server error (http 500) when I try to browse index.php in the directory of this .htaccess file. When I remove the two rewriterule statements, it works.
Not sure why, but no error shows up in the logs folder. There is no error log whatsoever.
Any clue on what's going wrong here?
Your expression, (.*), matches the target, /public/something. The rewrite engine loops until the request URI stops getting changed, and your request URI is turning into /public/public/public/public/public etc because there's nothing that stops the rewriting process. Try adding a condition to prevent the loop:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule (.*) public/$1 [L]
</IfModule>
So if the request already starts with /public, don't apply the rule.
Here are the rules:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ index.php?action=home [L]
RewriteRule ^[\w\W]*$ error.php [L]
When a page matches the first one, it is supposed to ignore any other further rules. Yet accessing / results in error.php being invoked. Commenting out the second rule works as intended - the page redirects to index.php.
What am I doing wrong?
Also: is there a better way to write the last line? It's basically a catch-all.
You could change
^[\w\W]*$ to ^[\w\W]+$ or ^.+$
If the last line is a catch-all, just do RewriteRule ^.*$ error.php [L]
Your first line might be erroring out when you send a request for / because your rule is saying "nothing" and you are sending /.
Try changing your rule to RewriteRule ^/$ index.php?action=home [L]