RewriteRule not working for one specific case - apache

I'm banging my head on the wall with this one. I have a rewrite rule in my htaccess file which works for all urls but one. Here's the line
RewriteRule ^([a-zA-Z0-9_-]+)$ content.php?linkid=$1 [L]
And the complete htacess
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
#RewriteRule ^contact-us content.php?linkid=contact-us [L,R]
RewriteRule ^([a-zA-Z0-9_-]+)$ content.php?linkid=$1 [L]
RewriteRule ^news/([a-zA-Z0-9_-]+)/([0-9]+)$ media.php?news=$1&pgNo=$2 [L]
RewriteRule ^news-details/([a-zA-Z0-9_-]+)$ media-detail.php?news_short=$1 [L]
RewriteRule ^services-list/([a-zA-Z0-9_-]+)$ services-list-new.php?services_list=$1 [L]
RewriteRule ^specialities/([a-zA-Z0-9_-]+)/([0-9]+)$ speciality-listing.php?speciality=$1&pgNo=$2 [L]
RewriteRule ^services-details/([a-zA-Z0-9_-]+)$ services.php?speciality_short=$1 [L]
RewriteRule ^treatment-detail/([a-zA-Z0-9_-]+)$ treatment-detail.php?treatment_short=$1 [L]
RewriteRule ^testimonial/([a-zA-Z0-9_-]+)/([0-9]+)$ testimonials.php?testimonial=$1&pgNo=$2 [L]
RewriteRule ^testimonial-detail/([a-zA-Z0-9_-]+)$ testimonial-detail.php?test_short=$1 [L]
RewriteRule ^photo-gallery-detail/([a-zA-Z0-9_-]+)$ photo-gallery-detail.php?photo_short=$1 [L]
RewriteRule ^media/([a-zA-Z0-9_-]+)/([0-9]+)$ media.php?media=$1&pgNo=$2 [L]
RewriteRule ^media-detail/([a-zA-Z0-9_-]+)$ media-detail.php?media_detail=$1 [L]
RewriteRule ^doctor-profile/([a-zA-Z0-9_-]+)$ doctor.php?doc_short=$1 [L]
RewriteRule ^make-an-appointment/([a-zA-Z0-9_-]+)$ make-an-appointmen.php?appointment_id=$1 [L]
RewriteRule ^current-openings/([a-zA-Z0-9_-]+)/([0-9]+)$ current-openings.php?current_opening=$1&pgNo=$2 [L]
</IfModule>
So this works for domain.com/privacy-policy which goes to domain.com/content.php?linkid=privacy-policy and a few others like testimonials, services, career etc
BUT, it doesn't work for contact-us, i.e. domain.com/contact-us
I've even tried the hard coded version above which is the commented line above. Any ideas guys?
EDIT:
I've tried using the url domain.com/content.php?linkid=contact-uswhich works correctly but domain.com/contact-us gives me the error The requested URL /contact-us was not found on this server..
I've also discovered another url that's not working and it is domain.com/media/list/1 which says The requested URL /media.html/list/1 was not found on this server. I don't know where it's getting media.html from. There was a file named media.html in the file structure so I removed that and the link started working. That's not the case with contact-us though.

Translating above comments into an answer as it might help someone with similar problems.
You need to disable MultiViews options to make it work.
So just change your Options line this:
Options +FollowSymlinks -MultiViews
Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So if /file is the URL then Apache will serve /file.html.

Related

Getting 404 error when viewing CakePHP 3 app on 1and1

When I attempt to hit my site which is at mydomain.com/subfolder, I get a 404 (from Apache).
/subfolder/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
/subfolder/webroot/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
To confirm that mod_rewrite works, I added this in the /subfolder/.htaccess file which promptly redirected:
Redirect 301 / http://other-domain-owned.com/
So, .htaccess are live and mod_rewrite works.
On my development environment, I was able to make the site work in the same /subfolder by symply enabling .htaccess files for that folder via a Directory directive.
Some tutorials say to add RewriteBase / but this didn't help.
Instead of RewriteBase, you can also use an absolute path. Additionally, the first two rules can be simplified to one
RewritRule ^(.*)$ /subfolder/webroot/$1 [L]
Similar the second part
RewriteRule ^ /subfolder/webroot/index.php [L]
Finally, Redirect and RewriteRule are from different modules. So, if one of the directives is working, this is no proof for the other one working too.

CakePHP - I keep getting 500 Internal Server Error messages, with the occasional 404 when trying to fix

I've been working with cakePHP for a while now, and there's an error that keeps appearing occasionally that I have been unable to find a fix for.
The issue is, in some servers cakephp seems to return a 500 Internal Server Error, even when the same software works in most other servers. This has become an issue because apparently the error is now appearing in GoDaddy servers, which, being the most common server, is what I would expect many of our customers to use. Also I know this isn't a coding issue since the error will appear both with our software and with new, empty cakePHP packages as downloaded from here.
That said, I have already tried changing the .htaccess files to fit what described here, to no avail. As of this moment the .htaccess files for the test (codeless, should only output a cakephp release notes page) install look like this:
The one on /caketest/ (That's the subfolder I'm installing on):
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
# Rewrite Base: The Base Path For Domain
RewriteBase /
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
The one on /caketest/app/:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1/ [L]
</IfModule>
The one on /caketest/app/webroot/:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
With this change to the default cakephp .htaccess files, the 500 Internal Server Error page disappears. However, now I get a 404 page saying:
The requested URL /app/webroot/ was not found on this server.
Then, I tried changing the /caketest/ .htaccess file to match the subfolder it's in:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
# Rewrite Base: The Base Path For Domain
RewriteBase /caketest/
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
But then I start getting the 500 Internal Server Error issue again. I've spent all morning looking over the web for possible solutions to this issue, but I can't seem to find any. Any threads have either gone unanswered or have the same solution I've already tried to no avail. I'm at the end of my wits here, can anyone help with this?
Just keep the default .htaccess. nothing more, nothing less.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
It will remove the statusCode:500 and you are good to continue..

Mod_rewrite - url rewriting isn't working

I've been trying to get the mod_rewrite in my .htaccess file to work for the past few hours but I can't seem to pull it off. The .htaccess files definitely work on my server because I've used them before.
I've tried to rewrite it with this generator and testing it with this tool as well as on my server.
URL
I need to turn
http://someurl.com/news-detail.html?id=10&news=this-is-an-article
into
http://someurl.com/news/10/this-is-an-article
or
http://someurl.com/10/news/this-is-an-article
Rewrite
I've tried:
RewriteEngine On
RewriteRule ^([^/]*)/news/([^/]*)$ /news-detail.html?id=$1&news=$2 [L]
and
RewriteEngine on
RewriteRule /(.*)/news/(.*) news-detail.html?id=$1&news=$2 [L]
and
RewriteEngine on
RewriteRule news/([0-9]+)/([a-zA-Z_]+)$ news-detail.html?id=$1&news=$2 [L]
and many others.
I hope someone can help me out here..
Kindly check/add RewriteBase in your .htaccess file.
Also check if htaccess is allowed to rewrite or not.
Syntax is below
RewriteEngine on
RewriteBase /flodername/
RewriteRule ^urlinbrowser/(.*)/(.*)$ filename.php?first_param=$1&secondparam=$2 [L]
Keep your rules like this in root .htaccess:
RewriteEngine On
RewriteBase /dev/hoig/
RewriteCond %{THE_REQUEST} /news-detail\.html\?id=([^\s&]+)&news=([^\s&]+) [NC]
RewriteRule ^ %1/news/%2? [R=302,L]
RewriteRule ^([^/]+)/news/([^/]+)/?$ news-detail.html?id=$1&news=$2 [L,NC,QSA]
This will support /10/news/this-is-an-article URI structure for pretty URLs.

.htaccess - Redirect to a subfolder according to the preferred language

we have a website in English. Now we want to have a translated copy of this website in German.
The index.html is located in the root folder: www.mydomain.com/index.html
Now I want to create a htaccess file which redirects all user which have set the preferred language in their browser to German (de, de-de, de-at, de-ch) to www.mydomain.com/de/index.html. All other user should be redirected to www.mydomain.com/en/index.html.
I tried a lot but I can't make it work. I do not really understand the regular expression thing and it's usage.
Most of the tries ended up with error 403 or 500. The current htaccess doesn't end up in an error, but it doesn't work.
The htaccess is now:
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP:Accept-Language} ^de [NC]
RewriteRule ^/(.*)$ /de/$1 [L]
RewriteRule ^/(.*)$ /en/$1 [L]
It seems like the file doesn't work at all. When I open www.mydomain.com it opens www.mydomain.com/index.html
Htaccess and rewriting works in general:
RewriteRule ^(.*)$ http://www.google.com/ [R=301,L]
works.
If I delete the slash behind ^
RewriteRule ^(.*)$ /en/$1 [L]
I get an error 500.
What do I do wrong?
I actually want the server to redirect to the webpage which the user tried to open. So if the user opens mydomain.com/index2.html he should be redirected to mydomain.com/xx/index2.html (and not to index.html)
Any ideas?
Thanks,
Jaroslaw
You can use this rule:
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteRule ^(en|de)/ - [L,NC]
RewriteCond %{HTTP:Accept-Language} ^de [NC]
RewriteRule ^(.*)$ /de/$1 [L]
RewriteRule ^(.*)$ /en/$1 [L]

godaddy .htaccess RewriteRule

I'm trying to set up a .htacess file on Godaddy webhosting ( apache-linux ofcourse, not IIS ). But i`m stuck with a problem:
RewriteEngine On
Options +FollowSymLinks
Rewriterule ^templates/.*$ - [PT]
Rewriterule ^controllers/.*$ - [PT]
Rewriterule ^.*$ index.php [NC,L]
I`m getting an Internal Server Error for line:
Rewriterule ^.*$ index.php [NC,L]
I don't know how to fix this, i've tried everything I know... basically I want to send anything that comes to index.php where a bootstrapper is set. This is working on any hosting i`ve ever tried, but godaddy seems to have problems with this: ^.*$ any help would be appreciated.
This rule
Rewriterule ^.*$ index.php [NC,L]
looks OK, but make sure that there are no spaces between the flags
i.e [NC, L] should be changed to [NC,L].
You can try the equivalent to see if it makes a difference (you don't need NC because it already matches any request)
Rewriterule .* index.php [L]
If that is not the issue, then it is likely the rules before that cause it to fail. Comment them both out and see if it works, but add a RewriteCond as below
Options +FollowSymLinks
RewriteEngine On
#if its not already index.php
RewriteCond %{REQUEST_URI} !index\.php$ [NC]
Rewriterule .* index.php [L]
If those rules are the real cause, then I would like to know what was your intent with those rules, and can they be expressed a different way i.e. if the intent was to rewrite all content except that in the templates or controllers directory, then that could be achieved by
#if request is not for templates or controllers directory
RewriteCond %{REQUEST_URI} !^/(templates|controllers)/ [NC]
Rewriterule .* index.php [L]