mod_rewrite causes 500 server error/Fails/works partially - .htaccess

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.

Related

rewrite some urls since menu change

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]

Force subset of webpages as HTTPS

I would like to force a subset of webpages to https and all other webpages as http.
In htaccess I use the following script that I found in another post, but that wasn't working...
RewriteCond %{HTTPS} off
RewriteRule ^(login|signup)\.php https://%{HTTP_HOST}%{REQUEST_URI} [R,L,QSA]
RewriteCond %{HTTPS} on
RewriteCond ${REQUEST_URI} !(login|signup)\.php
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R,L,QSA]
HTTP is forced as it should be, HTTPS is forced as it should be, but eg https://mywebsite.com/signup produces an infinite loop error in my browser. Any ideas what goes wrong?
I changed to code to the following which seems to work, but now the SSL is only partially implemented due to secure and insecure items on the webpage. I checked the URLS to e.g. images, style sheets and external javascript files bit these are all relative and shouldn't pose a problem... If someone knows how to deal with this I'd be glad to hear it.
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/login$ [OR]
RewriteCond %{REQUEST_URI} ^/signup$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !login$
RewriteCond %{REQUEST_URI} !signup$
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Try adding this line somewhere on top of your .htaccess:
Options +FollowSymLinks -MultiViews
Maybe you have some other rules that do this redirect -- it would be good if you provide whole contents of your .htaccess file.
You may have redirect inside the actual php script.
In any acse -- if you can edit Apache's config files (httpd.conf or httpd-vhost.conf) then you can enable rewrite debugging (RewriteLoglevel 9) and see what exactly is going on -- this is the best option (if you can).

Need help figuring out .htaccess rules

I'm pretty new to apache, and I need some help with some .htaccess rewrite rules. Here're the current rules:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/beta/index\.php$
RewriteRule ^(.*)$ /beta/index\.php [R=302,L]
Basically it redirects any requests to mydomain.com\beta\index.php, except if a filename's specified. This works fine for now.
Here's the problem:
I have a subdomain beta.mydomain.com which I don't want these rules to apply to. Any URL on beta.subdomain.com should be treated normally. However, since the .htaccess is at the server root, it's rules are messing with the beta.subdomain.com as well.
I've tried many different combinations of regex, but I can't figure it out. Help!
You need use something like this (to check if your request was related to specific domain/subdomain):
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com(:80)?$
20 seconds were missed... But my answer is better ;)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/beta/index\.php$
RewriteCond %{HTTP_HOST} !^beta\.subdomain\.com
RewriteRule ^(.*)$ /beta/index\.php [R=302,L]
This ought to do the trick!

Htaccess Rewrite Cond or redirectmatch to remove query string from front of URL

I remember that redirectmatch can't handle question marks but how can I match this url:
http://www.mysite.com/es/?lang=es&url=index.php&option=com_virtuemart&page=shop.browse&category_id=42&product_type_id=1&product_type_1etcetcetc`
to remove the lang=es&url= from before the index.php when there is a folder name present?
My problem would be solved if I could either remove the /es/ folder from the URL when presented with the ?lang=es&url= query string or I could remove the query string ?lang=es&url= from the URL when the folder is /es/
There are about 11 languages, with country codes fr, de, etc and one odd one out zh-CN. This is just past my capabilites at the moment. Thanks for taking the time to read this and any help would be greatly appreciated.
EDIT: mainly working now. I'm just having a small problem with the zh-CN language as it seems to be acting differently from the other en, fr, de etc, languages which are doing what I want, staying in English even when double clicking on another language. However, the zh-CN language redirects to the homepage with http://www.seed-city.com/?lang=zh-CN&url=index.php&zh-CN
I currently have this in my htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/../
RewriteCond %{QUERY_STRING} lang=..&url=index.php&(.*)
RewriteRule ^(.*)$ /$1index.php?%1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/(zh-CN|zh-TW)/ [NC]
RewriteCond %{QUERY_STRING} lang=([a-z]{2}|zh-CN|zh-TW)&url=index.php&(.*) [NC]
RewriteRule ^(.*)$ /$1index.php?%1 [R=301,L]
I have much more after but this is the relevant part. Thanks for your time. Natastna2.
If I understood your requirement properly this will work in your .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/es/
RewriteCond %{QUERY_STRING} lang=es&url=index.php&(.*)
RewriteRule ^(.*)$ /$1index.php?%1 [R=301,L]
Using above role a URL like this: http://www.mysite.com/es/?lang=es&url=index.php&option=com_virtuemart&page=shop.browse&category_id=42&product_type_id=1&product_type_1etc
will be redirected to:
http://www.mysite.com/es/index.php?option=com_virtuemart&page=shop.browse&category_id=42&product_type_id=1&product_type_1etc
EDIT
As per your edited section here are the rewrite rules that should work for now:
RewriteCond %{REQUEST_URI} ^/../
RewriteCond %{QUERY_STRING} lang=..&url=index.php&(.*)
RewriteRule ^(.*)$ /$1index.php?%1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/(zh-CN|zh-TW)/
RewriteCond %{QUERY_STRING} lang=(zh-CN|zh-TW)&url=index.php&(.*)
RewriteRule ^(.*)$ /$1index.php?%2 [R=301,L]

How can i get my htaccess to work (subdomains)?

I'm sorta a noob at these things but I'm trying to make a simple virtual subdomain with .htaccess. I have wildcard enabled and after lots of digging, this is what I've come up with:
rewriteEngine On
rewriteCond %{HTTP_HOST} !^$
rewriteCond %{HTTP_HOST} !^(www\.)?khpedia\.com$ [NC]
rewriteCond %{HTTP_HOST}<->%{REQUEST_URI} ^(www\.)?([^.]+).*<->/([^/]+) [NC]
rewriteCond %2<->%3 !^(.*)<->\1$ [NC]
rewriteRule ^(.+) /%2/$1 [L]
My directory is setup as
-root
--wiki
----index.php
--test
Right now when I travel to wiki.khpedia.com, I get a page not found. When I travel to wiki.khpedia.com/index.php, it travels to wiki.khpedia.com/wiki/index.php. I am somehow also able to access wiki.khpedia.com/test. If it doesnt seem obvious yet, I want to be able to go to wiki.khpedia.com/index.php and see wiki.khpedia.com/wiki/index.php but not in my address bar. Sorry for the text block and thanks for the help.
This works on the Apache side:
RewriteCond %{HTTP_HOST} !^www\.khpedia\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.+).khpedia\.com$ [NC]
RewriteRule ^(.*) /%1/$1 [L,QSA]
But you're wiki software might make up it's own mind about redirects / urls.
RewriteCond ^(.*)$ /wiki/$1 [L]
I'm confused about where subdomains come into your question. Could you give some examples of URLs you want to access in the browser and what they should point to at the server?
EDIT | Ah, I see now, Wrikken's answer handles the subdomains correctly :)

Resources