I'm trying to solve this for more than two hours now.
I have a personal site which uses .htaccess to manage urls.
It looks like this:
RewriteEngine on
RewriteBase /
...
RewriteRule ^sklad/?$ index.php?action=sklad
RewriteRule ^sklad/user/([0-9]+)?$ index.php?action=sklad&user=$1
RewriteRule ^sklad/folder/(.+)?$ index.php?action=sklad&folder=$1
RewriteRule ^sklad/file/(.+)?$ engine/ajax/sklad.php?file=$1
RewriteRule ^sklad/logout/?$ index.php?action=sklad&op=logout
...
RewriteRule ^admin/?$ admin.php
RewriteRule ^admin/news/?$ admin.php?action=news
the first five ones work fine. The admin/ one works fine. But when I try to access admin/news/, I get a blank page. No errors displayed or logged by Apache, and no output. admin.php?action=news is working fine.
Both sklad/ and admin/ folders physically exist on the server. BUT when I rename the admin/ folder to something else OR change the the last RewriteRule to something like
RewriteRule ^admin123/news/?$ admin.php?action=news
I can access admin123/news/. If it has something to do with the actual folder existing on the server, then why the first five rules are working? This doesn't make sense.
I'm out of ideas, hope someone here helps...
Yes, it's called news.php... I renamed the file and exerything is fine now, thanks! Didn't know about this, pretty unobvious bug (or not?)
It's not a bug, it sounds like content negotiation (via mod_negotiation) is turned on and it's doing something you don't want. Negotiation can be turned on via a type map or the MultiViews option. Typemaps are a little explicit to setup, so I'm assuming since you don't know why this is happening, you haven't set of a specific type that maps to news.php. So you've probably got Multiviews turned on. You can turn it off by either removing it from an Options statement:
# remove this word -----------v
Options Indexes FollowSymLinks Multiviews
This could be anywhere, in your htaccess, server config, vhost config, some config include file, etc. So you can also explicitly unset it in your htaccess file (as long as you aren't also explicitly setting it in the same file):
Options -Multiviews
I am not to good with htaccess and RegExp but i think admin/news will fall into your first htaccess rule.
RewriteRule ^admin/?$ admin.php
It wont proceed on your second rule on admin which is:
RewriteRule ^admin/news/?$ admin.php?action=news
Its the same problem I was facing before.
Try to modify your admin.php, echo/print something when there is no params pass. Something like this:
if($_GET['action'])
{
echo 'With Parameters';
}
else
{
echo 'No Parameters pass';
}
Debugged it that way.
Related
I'm just going to explain my problem here :
http://mysite.com/movie/20000
should be rewritten to
http://mysite.com/movie.php?id=20000
my htaccess file :
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^movie/([0-9]+)$ movie.php?id=$1
On my localhost WAMP installation this works fine, but when I put it online on my linux host it doesn't completely work. It does go to the movie.php page, but it seems it gives no GET parameter id.
Edit :
if I use
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^movie([0-9]+)$ movie.php?id=$1
then
http://mysite.com/movie20000
Goes to the correct page, but I would like /movie/20000 and not just /movie20000
It also seems that my host automatically rewrites a visit to mysite.com/movie to mysite.com/movie.php
After searching for a long time, and pulling some of my lovely hair out I found the solution.
I just added
Options -MultiViews
to my htaccess file and that fixed it.
Why? Because .php was being added to urls without an extension, which I really did not need.
This should work.
RewriteRule ^movie/([0-9]+)$ http://mysite.com/movie.php?id=$1 [NC,L]
Don't forget the [NC, L] it means Case insensitive, last rule... If you don't, it will continue to process through your htaccess rules, maybe triggering other ones.
Still, the stuff below is good advice.... :)
Check to see if the Rewrite module is loading with apache. Look for this line in the httpd.conf file:
LoadModule rewrite_module modules/mod_rewrite.so
Check to see if your apache config allows for .htaccess files for your system or in the virtual host definition.
Also, test with a simpler rewrite catch all and test that alone to see if it's working at all like this (get rid of everything else in your htaccess file to limit the test):
RewriteEngine On
RewriteRule ^(.*)$ http://www.google.com [L,R=301]
Any request to your site should go to google if the configuration for apache is correctly set.
I want to change my url with only the parameter. As I am passing only one parameter, so I want the sub url should be changed with my parameter name. I want change the url as the following type-
From:
http://www.xyz.com/cat.php?slag=season
to
http://www.xyz.com/season
Can anyone help me to do it. I don't know how to do it. Thanks
Options +FollowSymLinks
RewriteEngine On
RewriteBase /path/to/your/directory
RewriteRule ^(.*)cat/(.*)$ cat\.php?slag=$2&%{QUERY_STRING} [L]
put the above in your .htaccess file.. and that would do the magic..
Note :
RewriteBase /path/to/your/directory
(use this line only if your application is in any of subfolder the folder)
I suggest you obtain a copy of the .htaccess that WordPress uses. It's a quite powerful starting point that allows you to handle routing internally from within your Application - which you might feel more comfortable with, as it seems to me.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^mocks/site/(.*)$ http://thelivewebsite.com/$1 [R=301,L]
That is my htaccess file's contents.
The htaccess file is in the root directory of the hosting account and I just want to redirect the directory mocks/site/ to the new domain (with or without any extra directories).
eg: if someone goes to http://mywebsite.com/mocks/site then it needs to redirect to http://thelivewebsite.com. If they go to http://mywebsite.com/mocks/site/another/directory then it needs to redirect to http://thelivewebsite.com/another/directory. I hope that makes sense.
So the problem I have is that the htaccess code above seems to work pretty well when there is something after mocks/site/ however when there isn't something after that then the $1 in the redirect seems to reference the whole REQUEST_URI (eg: mocks/site/ rather than nothing - as there is nothing after it).
I don't know how to stop this. I thought about using a RewriteCond, but I'm not sure what to use there. I can't find anything that helps me to determine if there is anything after mocks/site/ or not.
Any help will be much appreciated.
Thank you.
That's very strange behaviour -- never seen anything like that. Therefore I think it could be something else (another rule somewhere -- on old or even new site). I recommend enabling rewrite debugging (RewriteLogLevel 9) and check the rewrite log (that's if you can edit Apache's config file / virtual host definition).
In any case, try this combination:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^mocks/site/$ http://thelivewebsite.com/ [R=301,L]
RewriteRule ^mocks/site/(.+)$ http://thelivewebsite.com/$1 [R=301,L]
It will do matching/redirecting in 2 steps: first rule is for exact directory match (so no $1 involved at all) and 2nd will work if there is at least 1 character after the /mocks/site/.
Alternatively (Apache docs even recommending this one) use Redirect directive (no need for mod_rewrite at all for such simple redirects):
Redirect 301 /mocks/site/ http://thelivewebsite.com/
Here's a problem I'm always wanting to solve with htaccess. I haven't found a way yet, though it looks like it should be possible - perhaps someone can help.
Let's say I have a folder at the root of my site called /foo/. I want users to be able to access that folder at the path /bar/, but for various reasons I can't rename the folder.
So as not to create confusion I only want one path to ever be seen - that is to say, I don't want people to use the name /foo/ to access the folder; they should always use /bar/. If someone goes to example.com/foo/, their browser should redirect to example.com/bar/ - but the content returned should be the content of /foo/.
To make matters more complicated, pages in /foo/ have dependencies (images, stylesheets, links to other pages, etc) within /foo/ which are hardcoded and can't be changed. These must, of course, still work.
So, to summarise, this is what I want :
Requests for example.com/foo/ should redirect to example.com/bar/.
Requests for example.com/bar/ should return the contents of example.com/foo/.
Is this possible? It looks on the surface as if it would create an infinite redirect... but I'm pretty sure there are ways to prevent that in htaccess, aren't there?
I'd be very grateful for any help.
(PS - for a little extra background: The normal reason I want to do this is to rename the wordpress /wp-admin/ directory to something more professional and easy for customers to remember, such as /admin/. But the same system should work for masking any path in this way.)
I found a sort of workaround - by using a symlink and htaccess in combination.
First I created a symlink from /bar to /foo/.
Then I put this in htaccess :
Options +FollowSymLinks
RewriteRule ^foo/(.*)$ bar/$1 [R,L]
This has exactly the desired result - example.com/bar/ shows the content of the /foo/ directory, and example.com/foo/ redirects to example.com/bar/
But if anyone can come up with a pure htaccess solution I'd much prefer that!
Update :
Ok, I've finally found out how to do this. It turns out to be quite simple...
RewriteCond %{THE_REQUEST} ^GET\ /foo/
RewriteRule ^foo/(.*)$ bar/$1 [R,L]
RewriteRule ^bar/(.*)$ foo/$1
The only problem is that it doesn't take account of RewriteBase, so you have to include the full path in the first line (after ^GET\).
If I understand correctly what you want is something like this inside your .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^foo/$ bar/
RewriteRule ^bar/$ foo/
</IfModule>
I have a couple of rewrite rules in htaccess. They work on one server but not another. My script is as follows (I've commented out how the urls look):
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/images/
#example.com/regions/fife/
RewriteRule ^regions/([A-Za-z0-9\-\+\']+)/?$ /regions.php?region=$1 [L]
#example.com/regions/fife/dunfermline
RewriteRule ^regions/([^/]+)/([^/]+)$ /regions.php?region=$1&town=$2 [L]
It returns two variables (region & town) I can manipulate in PHP, and throw up the appropriate content. I have a Rackspace server, and the script works perfectly, but on another server (Freedom2surf) it only works so far. It doesn't return the variables. I get a blank $_GET array...
Any ideas? F2S aren't giving me any clues, just that I should check my code. But if it works on another server, then what gives? Is it an Apache setting that is different?
I think you may be after the 'QSA' flag, which will append the query string from the original request to the redirected request, e.g:
#example.com/regions/fife/
RewriteRule ^regions/([A-Za-z0-9\-\+\']+)/?$ /regions.php?region=$1 [L,QSA]
This sounds like you have a mod_negotiation conflict here and you need to turn Multiviews off. Sometimes apache default configurations have Multiviews turned on by default. What that does is it will look at a request, say, /regions/1234 and mod_negotiation will notice that there is a file /regions.php and assume that the request is actually for that php file. It'll thus serve /regions.php/1234 and completely bypass mod_rewrite. You can use Options to turn it off. Just add this to the top of your htaccess file:
Options -Multiviews