- This topic is empty.
-
AuthorPosts
-
May 25, 2014 at 7:27 pm #171072ewiselyParticipant
Use REQUEST_URI when you want to compare a pattern to the URI. Use REQUEST_FILENAME when you want the filename.
Think I slightly get what you mean about URI and FILENAME. I’ll definitely need more time to explore the use of these 2 requests in order to understand more on how different they behave in the process of mod-rewrite. Thanks for that.
(if your routine/ files will always have a dash in the name, and there will never be other files with dashes in the name, then you might be able to write a rule to recognize that. But that’s a big “if,” and I would not recommend this approach.)
I take your advice. I don’t want to get complicated with this mod_rewrite thing too since I just started to learn it 10 days ago. By the way, I saw this:
RewriteRule ^([a-zA-Z_-\s]+)/$ /$1.html
What does that
_-\s
do?May 25, 2014 at 9:44 pm #171073__ParticipantMay 25, 2014 at 11:19 pm #171075ewiselyParticipantHi traq,
Understood. Thank you very very much for your great patience and help. I’ve learned a lot from you. :D
May 26, 2014 at 8:55 am #171090__ParticipantNo problem, glad you’re learning.
July 27, 2014 at 9:16 pm #176637ewiselyParticipantHi traq,
I’m having problem doing 301 redirects. As I have just changed my site’s url structure from:
mysite.com/q/sleep-rest.htm to mysite.com/sleep-rest/
mysite.com/q/play-study.htm to mysite.com/play-study/
and so on…I need to redirect them all with just a single line of code. So, I do this:
RewriteRule ^q/([a-zA-Z0-9_-]+)\.htm$ /$1/ [R=301,L]
<code></code>But I got a “redirect loop” error. What’s wrong with my code? Appreciate your help. Thanks.
July 28, 2014 at 1:10 pm #176713__ParticipantI’m not sure exactly what is causing it, but a “redirect loop” is where you have rewrite rules that create a URL that matches (the same or another) rewrite rule, so the URL is rewritten constantly, without end.
Is that the only rewrite rule you’re using?
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
To address your problem, you have a file at (for example):
/q/sleep-rest.htm
and you want to use the following URL to access it:
mysite.com/sleep-rest/
Correct?
RewriteRule ^/([a-zA-Z0-9_-]+)/?$ /q/$1.htm
July 28, 2014 at 6:58 pm #176721ewiselyParticipantSorry for not being clear.
My site url (under /q/) used to be like that:
mysite.com/q/sleep-rest.htm
mysite.com/q/play-study.htm
and so on…I’ve used
RewriteRule ^/([a-zA-Z0-9_-]+)/?$ /q/$1.htm
so that when people click on (or type into the browser)mysite.com/sleep-rest/
ormysite.com/sleep-rest
, both will retrieve content frommysite.com/q/sleep-rest.htm
However, because this site is pretty old and has quite a number of old urls pointing back from other websites. So, I want to do a permanent redirect of
mysite.com/q/sleep-rest.htm
tomysite.com/sleep-rest/
and likewise for the rest of the urls to avoid creating confusion to my visitors.Since my files are all under /q/ folder, I’m trying to use a single line of code (
RewriteRule ^q/([a-zA-Z0-9_-]+)\.htm$ /$1/ [R=301,L]
) to perform the 301 redirect for all old urls but it invokes a redirect loop.Is it actually possible to work out something like that? Appreciate your help.
July 28, 2014 at 9:18 pm #176725__ParticipantAhh… so, you’ve got one rule converting from form A→B, and then another converting from form B→A. See the infinite loop?
Instead of trying to match the legacy URL, use a rewrite condition to check against the actual request, so you only rewrite it when needed:
RewriteCond %{THE_REQUEST} /q/([\w-]+)\.htm RewriteRule ^.*$ /%1 [R=301] # follow with other rules as normal RewriteRule ^([\w-]+)/?$ /q/$1.htm
July 28, 2014 at 10:51 pm #176731ewiselyParticipantThe code works like magic!!
RewriteCond %{THE_REQUEST} /q/([\w-]+)\.htm RewriteRule ^.*$ /%1 [R=301]
But could you explain a little more about what the code does, so that I can learn how to use
%{THE_REQUEST}
,\w-
and/%1
which look totally Greek to me. Thank you very much. :)July 30, 2014 at 12:06 am #176878__Participant%{THE_REQUEST}
is an Apache variable. Its value is the actual request line* from the HTTP request, unchanged by any rewrites (which is the reason it does not cause a redirect loop).* for example:
GET /index.html HTTP/1.1
\w-
is actually-
(a dash) and\w
(a “word” character; meaning alphanumerics or an underscore). Put together,[\w-]
is just a shorter, more succinct way to write[a-zA-Z0-9_-]
.%1
is a numbered backreference, just like$1
would be. The difference is that%
backreferences refer to capture groups inRewriteCond
s, while$
backreferences refer to capture groups inRewriteRule
s.July 30, 2014 at 1:34 am #176886ewiselyParticipantThanks for your clear explanation and reference. I really appreciate your passion in helping people like me. Thank you very very much. Have a nice day every day. :)
-
AuthorPosts
- The forum ‘Other’ is closed to new topics and replies.