rewrite some urls since menu change - .htaccess

I am having trouble getting
site.com/member-videos
into
site.com/videos
or in reality
#RewriteCond %{HTTP_HOST} !^/community/member-videos/
#RewriteRule (.*) http://site.com/community/videos/$1 [R=301,L]
this seems to loop !
for me the above says find the match for '/community/member-videos/' and send anything like that to the url after...I have lost too much time now on this typical I would guess issue and looked at a lot of similar online but nothing quite works for this case..
thanks

RewriteRule (.*) http://site.com/community/videos/$1 [R=301,L]
This is wrong, it will get you into an infinite loop.
Try this one:
RewriteRule http://site.com/community/member-videos/$1 ^community/videos/(.*)$ [R=301,L]

You almost had it. The variable your condition matches against is a host, and not a URI. If you need to check the host, it must only be the hostname (not URI path at all), e.g. site.com. You need to use a %{REQUEST_URI} variable for what you are matching against:
RewriteCond %{HTTP_HOST} ^(www\.)?site\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/videos/
RewriteRule ^/?member-videos(.*)$ /videos$1 [L,R=301]
The example you gave doesn't have the community part in the URI at all, if it's not in the URL, it won't appear in the string sent to the rewrite engine. But your rules has /community in them, so if in fact you need that path, then:
RewriteCond %{HTTP_HOST} ^(www\.)?site\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/community/videos/
RewriteRule ^/?community/member-videos(.*)$ /community/videos$1 [L,R=301]

Related

mod_rewrite causes 500 server error/Fails/works partially

I am trying to do a permanent redirect from my old design (www.your-translations.com) to my new design at www.your-translations.com/_YT.
In the process, the urls have to change from this format:
www.your-translations.com/my_page.php
to this format:
www.your-translations.com/_YT/index.php?pge=my_page
I have made many attempts so far, the latest of which is:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^.*your\-translations.com.*$ [NC]
RewriteRule ^(.*?)/([^/]*)\.php $1/_YT/index.php?pge=$2 [R=302]
RewriteCond %{HTTP_HOST} ^your-translations.com [NC]
RewriteRule ^(.*)$ http://www.your-translations.com/$1 [R=302,L]
This is one of my better tries in that it apparently does almost nothing (previous tries have resulted in 505 errors and infinite loops). The rewriting of URLs to "www." works properly.
I have tested the regex in different regex testers and it seems to do what I expect it to do.
Can someone explain what I am doing wrong?
WHAT exactly is the pattern applied to? Is it what is matched by the RewriteCond? The URI as I typed it in? The relative path? The actual path on the server?
Is there any way to display the string before I try to match it?
Several suggestions found in tutorials result in infinite loops on my server. Do all versions work the same way?
Unfortunately, the webhost technical support doesn't seem to know anything about mod_rewrite, so I can't expect any help from them.
According to my FTP log, I have already made about 80 attempts and I could really use a hand here.
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^your-translations\.com$
RewriteCond %{REQUEST_URI} !^/_YT/([a-z0-9-_]+)\.php$
RewriteCond %{REQUEST_URI} ^/([a-z0-9-_]+)\.php$
RewriteRule ^(.*) /_YT/index.php?pge=%1 [QSA]
RewriteCond %{HTTP_HOST} ^www\.your-translations\.com$
RewriteCond %{REQUEST_URI} !^/_YT/([a-z0-9-_]+)\.php$
RewriteCond %{REQUEST_URI} ^/([a-z0-9-_]+)\.php$
RewriteRule ^(.*) /_YT/index.php?pge=%1 [QSA]
In RewriteCond, you must type a backslash before a dot and not before dash, because a dot in .htaccess means any character, so you need bslash to call is as dot. Sorry, I'm in mobile, so I couldn't explain more, but feel free to ask what you want to know about the code.
Thanks to some explanations from webmasterworld, I was able to fix my mod_rewrite code:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^your-translations.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.your-translations.com [NC]
RewriteCond %{REQUEST_URI} !="^.*?/.*"
RewriteRule ^([^/]*)\.php$ http://www.your-translations.com/_YT/index.php?pge=$1 [R=302,L]
RewriteRule ^$ http://www.your-translations.com/_YT/ [R=302,L]
This seems to work correctly so far (touching wood). Thanks Servant for your help.

Multipart htaccess redirect

Hey guys I'm having a bit of trouble getting my htaccess to redirect properly and was hoping for some help.
I'm expecting DEV-domain.com?CampID=AB12345 to redirect to
http://DEV-www.domain.com/landing/external-marketing/direct-mail/AB?CampId=AB12345
RewriteCond %{HTTP_HOST} ^DEV-(www\.)?domain\.com [NC]
RewriteCond %{QUERY_STRING} ^CampID=
RewriteRule (\w{2})(\w{5})$ http://DEV-www\.domain\.com/landing/external-marketing/direct-mail/$1?CampId=$1$2 [R=301,L]
Unfortunetly I can't get it working for some reason?
Because the RewriteRule matching is meant for the url path, not query strings. Try this:
RewriteCond %{HTTP_HOST} ^DEV-(www\.)?domain\.com [NC]
RewriteCond %{QUERY_STRING} ^CampID=(\w{2})(\w{5})
RewriteRule .* http://DEV-www.domain.com/landing/external-marketing/direct-mail/%1?CampId=%1%2 [R=301,L]
also you don't need to escape dots . in the target url, only in matching patterns. And be aware that if you decide to make your target url CampID instead of CampId, you need to put in another condition:
RewriteCond %{REQUEST_URI} !^/landing/external-marketing/direct-mail/
to avoid an infinite redirect as a target with CampID would match your RewriteCond rule...

Rewrite specific URL to different directory

Im trying to redirect this,
example.com/slide?page=4 to example.com/slide/issue43?page=4
But it cannot effect other URL's like, example.com/slide/issue57?page=4
Im really stuck, these regular expressions are so weird. Here's the rewriterule that I've come up with,
This is not working
RewriteRule ^slide?page(.*)$ http://example.com/slide/issue43?page=$1 [L,R=301]
I need to target 'slide?page=X' specifically and have it redirect or point to 'slide/issue43?page=X'
This should work for you:
RewriteCond %{REQUEST_URI} ^/slide$
RewriteCond %{QUERY_STRING} ^page=(.*)$
RewriteRule ^(.*) /slide/issue57?page=%1 [R=301,L]

Creating a mod_rewrite rule in an htaccess file

I am trying to create a mod_rewrite rule to direct people to a sub-folder. Currently the code looks as follows:
RewriteEngine On
RewriteCond %{HTTP_HOST} abcsite.com$ [OR,NC]
RewriteCond %{HTTP_HOST} ^!www\.abcsite\.*$
RewriteCond %{REQUEST_URI} !^/abc/.*$
RewriteRule (.*)$ /abc/$1 [L]
The redirect works if the user types www.abcsite.com, but not if they type abc.com. Is there something that I am missing or should do differently to make sure the user goes to the correct folder (regardless of how they type the URL)?
Side note: The htaccess file that I am dealing with is a Joomla file, so all contents of it deal with another Joomla site. I appreciate the help.
Because you have conditions for that.
RewriteCond %{HTTP_HOST} abcsite.com$ [OR,NC]
RewriteCond %{HTTP_HOST} ^!www\.abcsite\.*$
RewriteCond %{REQUEST_URI} !^/abc/.*$
All above rules will pass only its abcsite.com
You add following rules also then it work for abc.com too.
RewriteCond %{HTTP_HOST} abc.com$ [OR,NC]
RewriteCond %{HTTP_HOST} ^!www\.abc\.*$
RewriteCond %{REQUEST_URI} !^/abc/.*$
RewriteRule (.*)$ /abc/$1 [L]
There's a stray ! in your second condition. A ! in front of the pattern means that the condition is true when the regex doesn't match (like in the third condition). A ! inside the pattern is just a literal symbol.
The host conditions should be something like:
RewriteCond %{HTTP_HOST} ^abcsite\.com$ [OR,NC]
RewriteCond %{HTTP_HOST} ^www\.abcsite\.com$ [NC]
And in fact, they can be joined into a single condition (note, no [OR] here):
RewriteCond %{HTTP_HOST} ^(www\.)?abcsite\.com$ [NC]
Your third condition is intended to prevent redirect loops (/foo → /abc/foo → /abc/abc/foo → …). What it says is that the rule isn't applied if the request URL starts with /abc/. However, your actual redirect is an internal redirect: if a user accesses abcsite.com/foo, the server internally rewrites this to /webroot/abc/foo, but REQUEST_URI stays the same, /foo.
The reason this doesn't cause a redirect loop as it is is likely rewrite rules in abc/.htaccess which override this one once the redirect is done.
What should be checked instead in the third condition is the path matched by the rewrite rule:
RewriteCond $1 !^abc/
RewriteRule (.*) /abc/$1 [L]

Help with .htaccess RewriteRule. Need to take user from one URL to other

I want users who type
http://www.example.com/word-of-the-day
to be taken to
http://www.example.com/index.php?page=word-of-the-day
But I want
http://www.example.com/word-of-the-day
to be shown in the URL for the user.
What should I do in my .htaccess? I tried but the regular expression
and the syntax of RewriteRule is way too complicated for me to
figure out how to do it.
Any help will be appreciated.
Edit:
Also, how can I say this in htaccess -
if they type http://www.example.com/word-of-the-day, take them to http://www.example.com/index.php?page=word-of-the-day
or if they type http://www.example.com/something-else, take them to http://www.example.com/index.php?page=something-else
or else, just take them to the URL they typed.
The condition below checks that index.php is not being requested. If not apply the rule. This will work for any of the scenarios you listed above.
RewriteCond %{QUERY_STRING} ^!.*[index\.php].*$ [NC]
RewriteRule ^(.*)$ index.php?page=$1 [L]
In response to your comment about only wanting to do this for a few specific pages, it would look like this(as an alternative to Nils edit):
RewriteCond %{QUERY_STRING} ^!.*[index\.php].*$ [NC]
RewriteCond %{REQUEST_URI} ^word-of-the-day$ [OR]
RewriteCond %{REQUEST_URI} ^something-else$ [OR]
RewriteCond %{REQUEST_URI} ^even-something-else$
RewriteRule ^(.*)$ index.php?page=$1 [L]
Try this
RewriteEngine on
RewriteRule ^word-of-the-day$ index.php?page=word-of-the-day
Or more flexible
RewriteEngine on
RewriteRule ^(.*)$ index.php?page=$1
Not tested, yet it sould work.
To your edit:
Just define those specific URLs manually:
RewriteEngine on
RewriteRule ^word-of-the-day$ index.php?page=word-of-the-day
RewriteRule ^word-some-example$ index.php?page=some-example
RewriteRule ^some-other$ index.php?page=some-other

Resources