RewriteRule not working without R flag - .htaccess

I need this URL :
http://example.net/aroundtheshows/get?id=123
to be rewritten (not redirected) to :
http://example.net/ats/get?id=123
so I added the following RewriteRule in my htaccess :
RewriteCond %{REQUEST_URI} ^\/aroundtheshows
RewriteRule ^aroundtheshows(.*)?$ ats$1 [L]
But it does not work, I get a 404.
However if I had the R=301 flag, the redirection works properly (though that's not what I want, but it means the match is correct).
thanks !

If I understand you correctly you are looking to visit
http://example.net/ats/get?id=123 and have it masked as that URL while serving content from http://example.net/aroundtheshows/get?id=123?
In that case simply inverse your rule - no need for the RewriteCond:
RewriteRule ^ats(.*) aroundtheshows$1 [L]
Demo here: http://htaccess.mwl.be?share=1392f5a6-c631-5e90-9ef5-6622644ed517

Related

htaccess file seems to pick up 'if contains' for a RewriteRule

We currently have a .htaccess RewriteRule that's incorrectly (or correctly as the rule is incorrect) redirecting a URL.
The Rule
RewriteRule ^holiday-ecards/?.*$ /appindex.php [L]
The desired redirects for this are:
http://domain.com/holiday-ecards/
http://domain.com/holiday-ecards/1/
http://domain.com/holiday-ecards/1/2
http://domain.com/holiday-ecards/1/2/3
However, it seems to also be redirecting the following, which is undesired:
http://domain.com/holiday-ecards-business/
EDIT
/appindex.php
This is taking care of the app routing and works as intended.
A number of ways you could do it, one would be setting a rewrite condition to not touch URI's that have holiday-ecards plus hyphen, like so:
RewriteCond %{REQUEST_URI} !^/holiday-ecards-.*$
RewriteRule ^holiday-ecards/?.*$ /appindex.php [L]
Not sure how many variations you have of URI's with holiday-ecards in them.
RewriteRule ^holiday-ecards/?.*$ /appindex.php [L]
(Note that this is an internal rewrite, not a redirect.)
The above RewriteRule pattern makes the slash after holiday-ecards optional (so it will also match holiday-ecards-business). However, in the example URLs that should be rewritten, the slash is mandatory. So, it would appear that you just need to make it mandatory (?), for example:
RewriteRule ^holiday-ecards/ /appindex.php [L]
The trailing pattern .*$ is superfluous.

Troubleshooting mod_rewrite in a .htacces with a LightSpeed

I have a couple web pages located at these locations:
Home Page / Index : www.codeliger.com/index.php?page=home
Education : www.codeliger.com/index.php?page=home&filter=1
Skills: www.codeliger.com/index.php?page=home&filter=2
Projects: www.codeliger.com/index.php?page=home&filter=3
Work Experience: www.codeliger.com/index.php?page=home&filter=4
Contact : www.codeliger.com/index.php?page=contact
I am trying to rewrite them to prettier urls:
codeliger.com/home
codeliger.com/education
codeliger.com/skills
codeliger.com/projects
codeliger.com/experience
codeliger.com/contact
I have confirmed that my htaccess file works and mod-rewrite works to google, but I cannot get my syntax working that was specified in multiple tutorials online.
RewriteEngine on
RewriteRule /home /index.php?page=home
RewriteRule /([a-Z]+) /index.php?page=$1
RewriteRule /education /index.php?page=home&filter=1
RewriteRule /skills /index.php?page=home&filter=2
RewriteRule /projects /index.php?page=home&filter=3
RewriteRule /experience /index.php?page=home&filter=4
How can I fix my syntax to rewrite these pages to prettier urls?
The first thing you should probably do is fix your regex. You cannot have a range like [a-Z], you can just do [a-z] and use the [NC] (no case) flag. Also, you want this rule at the very end since it'll match requests for /projects which will make it so the rule further down will never get applied. Then, you want to get rid of all your leading slashes. Lastly, you want a boundary for your regex, otherwise it'll match index.php and cause another error.
So:
RewriteEngine on
RewriteRule ^home /index.php?page=home
RewriteRule ^education /index.php?page=home&filter=1
RewriteRule ^skills /index.php?page=home&filter=2
RewriteRule ^projects /index.php?page=home&filter=3
RewriteRule ^experience /index.php?page=home&filter=4
RewriteRule ^([a-z]+)$ /index.php?page=$1 [NC]

link redirection using htaccess RewriteRule

I have these links in my website:
www.example.org/folder/files.php?file=folder/document.pdf
www.example.org/folder/files.php?force&file=2009.pdf
and I want redirect to :
www.example.org/files/folder/document.pdf
www.example.org/files/2009.pdf
I tried :
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^files/(.*)$ /files.php?file=$1 [R=301,L]
</IfModule>
but doesn't work!
any help?
RewriteRule ^files/(.*)$ /files.php?file=$1 [R=301,L]
There are two issues with this rule ... first, what you are matching needs to appear first in the rule, then what you are rewriting appears second - you have that backwards.
Once you reverse that, though, you run into the second issue - you can't match query strings in a RewriteRule, you need to match them in a RewriteCond:
To match www.example.org/folder/files.php?force&file=2009.pdf and redirect it to www.example.org/files/2009.pdf you would do:
RewriteCond %{QUERY_STRING} ^force&file=(.*)$ [NC]
RewriteRule ^folder/files.php$ /files/%1 [R=301, L]
The %1 matches what's in the parentheses in the RewriteCond.
Search on google first. The first thing displayed on google for htaccess is htaccess redirect. I think
Redirect /olddirectory/oldfile.html http://example.com/newdirectory/newfile.html (same line with a space) should work. Go to http://kb.mediatemple.net/questions/242/How+do+I+redirect+my+site+using+a+.htaccess+file%3F . Php would also do the work. Just goolgle things before asking them.

RewriteRule does not work, while the rest do

My blog's .htaccess is setup in such a way that one page is accessed through multiple URLs, and displays different content depending on which URL is visited.
http://kn3rdmeister.com/category/blog/
http://kn3rdmeister.com/2012/
http://kn3rdmeister.com/2012/07/
all are actually using http://kn3rdmeister.com/blog.php.
The .htaccess file is very handy in the sense that I only need to redirect to one page (pretty much ever) just with different query strings. After a lot messing around with 'em, all of my rules finally work, and I'm dang glad that they do. Well, almost all of them work. The last one does not.
the .htaccess:
RewriteEngine On
RewriteRule ^blog\.php$ /category/blog/ [R=301,L]
RewriteRule ^category/blog/?$ blog.php [L]
RewriteRule ^category/blog/page/?$ /category/blog/ [R=301,L]
RewriteRule ^category/blog/page/([0-9]*)/?$ /category/blog/?pagenum=$1 [L]
RewriteRule ^([0-9]{4})/?$ /category/blog/?year=$1 [L]
RewriteRule ^([0-9]{4})/([0-9]{2})/?$ /category/blog/?year=$1&month=$2 [L]
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/?$ /category/blog/?year=$1&month=$2&day=$3 [L]
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/(^/]+)/?$ /category/blog/?url=http://kn3rdmeister.com/$1/$2/$3/$4/ [L]
The last rule is supposed to redirect to the "permanent link" page for each blog post. Being that each URL is unique, I'm using the post URLs as the unique identifier. Essentially, it is supposed to pass the "url" query string through "blog.php". The PHP script takes over, sees that the "url" query string is set, and then loads the only post with that exact URL in it's row.
The script works, but the redirect doesn't. Going directly to
http://kn3rdmeister.com/blog.php?url=http://kn3rdmeister.com/2012/07/04/amsterdam-ave/
will load the right content. However, going to
http://kn3rdmeister.com/2012/07/04/amsterdam-ave/
doesn't.
Try adding QSA (Query String Append). Also, invert rules so that "deeper" links go on top.
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/(^/]+)/?$ /category/blog/?url=http://kn3rdmeister.com/$1/$2/$3/$4/ [QSA,L]
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/?$ /category/blog/?year=$1&month=$2&day=$3 [QSA,L]
RewriteRule ^([0-9]{4})/([0-9]{2})/?$ /category/blog/?year=$1&month=$2 [QSA,L]
RewriteRule ^([0-9]{4})/?$ /category/blog/?year=$1 [QSA,L]
But, you can't use rewritten links in other rules. So wherever you have category/blog/ replace it with blog.php.
Whilst webarto comments are good advice, your problem is a missing [:
^([0-9]{4})/([0-9]{2})/([0-9]{2})/([^/]+)/?$
not
^([0-9]{4})/([0-9]{2})/([0-9]{2})/(^/]+)/?$

.htaccess 301 redirect problem

i try to redirect url's like:
example.com/video/1640/video-name
to
example.com/video/1640/video-name/
i've tried with:
RewriteRule ^video/([^/]*)/([^/]*)/$ video.php?id=$1&title=$2 [L]
RewriteRule ^video/([^/]*)/([^/]*)$ /video/([^/]*)/([^/]*)/ [R=301,L]
but it is not working
my currently htaccess file has only the first line:
RewriteRule ^video/([^/]*)/([^/]*)/$ video.php?id=$1&title=$2 [L]
and videos only loads at
example.com/video/1640/video-name/
url type
i want to redirect the non-backslash url type
example.com/video/1640/video-name
to the correct one (the one with the backslash)
How can i do this?
Your second rule should be RewriteRule ^video/([^/]*)/([^/]*)$ /video/$1/$2/ [R=301,L]
Or you could forgo the redirect totally, and just say RewriteRule ^video/([^/]*)/([^/]*)/?$ video.php?id=$1&title=$2 [L] which will allow both to view your video.
Update FallingBullets is right (see the comments on this answer), his answer better suites the OP's problem, so please ignore this answer (I am leaving it for reference, though).
Maybe you simply have to prefix your pattern with a /?? E. g.
RewriteRule ^/?video/([^/]*)/([^/]*)/$ video.php?id=$1&title=$2 [L]
RewriteRule ^/?video/([^/]*)/([^/]*)$ /video/([^/]*)/([^/]*)/ [R=301,L]
# ^ these ones
instead of
RewriteRule ^video/([^/]*)/([^/]*)/$ video.php?id=$1&title=$2 [L]
RewriteRule ^video/([^/]*)/([^/]*)$ /video/([^/]*)/([^/]*)/ [R=301,L]
since you are anchoring the pattern at the beginning of the path (using ^).

Resources