friend url on wildcard subdomains htacces, not working - .htaccess

i have wildcard subdomains sets already and works fine, now i wish have friends url for the content in thats subdomains, the structure of my site is if the user type subdomain.maindomain.com and the .htaccess redirect to
blogs/index.php?user=subdomain
where blogs/index.php receive the param and show the correct content
now i try to make the url function like this
subdomain.maindoamin.com/24/title-of-content
and then .htaccess must result
blogs/index.php?id_content=24&title=title-of-content
i have the next .htaccess
Options +FollowSymLinks
#this force to server the content always without www.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$
RewriteRule ^(.*)$ http://%1/$1 [R=301]
#this is to pass the subdomain like param and show the right content of the user
RewriteCond %{HTTP_HOST} !^www\.misite\.com [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9]+)\.misite\.com
RewriteRule ^(.*)$ blogs/index.php?url=%1 [QSA,L]
#the next line i can't make work to make nice url
RewriteRule ^/(.*)/(.*)$ blogs/index.php?idP=$1&name=$2 [L]
not working because when i make in index.php
echo $_SERVER['REQUEST_URI'];
don't show idP=24 show /24/title-of-content and i need $_GET(idP)
i really apreciate some light on this stuff i am not expert on htaccess, thanks in advance to everybody.

There are two problems:
The first argument of RewriteRule matches against everything after the slash of the directory .htaccess is in, and before the query string. If .htaccess is in your www-root, and you get the url http://www.example.com/shiny/unicorns.php?are=shiny, you match against shiny/unicorns.php. It will never start with a slash, so ^/ will never match.
Rules are executed in order. If you go to http://sub.example.com/10/unicorns, the second rule will match first and rewrite the request to /blogs/index.php?url=10/unicorns. If you removed the leading slash the third rule would match, but normally you wouldn't want that. You want to have the third rule only match
You want to move the third rule up so it is the second rule. You want to make it more specific to only match with subdomains. You also know the first part contains only numbers, so use that knowledge to prevent blogs/index.php from matching your now second rule. You also need to prevent blogs/index.php from matching the now third rule to prevent it from matching itself. Last but not least I removed [L] from the now second rule, since the third rule will match anyway.
#the next line i can't make work to make nice url
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^([0-9]+)/([^/]+)$ blogs/index.php?idP=$1&name=$2
#this is to pass the subdomain like param and show the right content of the user
RewriteCond %{HTTP_HOST} !^www\.misite\.com [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9]+)\.misite\.com
RewriteCond %{REQUEST_URI} !/blogs/index\.php
RewriteRule ^ blogs/index.php?url=%1 [QSA,L]

Related

mod_rewrite and redirect causing loop

I have problem when I try to redirect and rewrite together.
I have site example.com/show_table.php?table=12 (max 99 tables). I wanted nice links, so I got this .htacces rw rule:
RewriteRule ^table/([0-9]{1,2})$ show_table.php?table=$1 [L,NC]
Now are links something like example.com/table/12 - it's definitely OK. But I want all old links redirect to new format. So I use Redirect 301, I added to .htaccess this code:
RewriteCond %{REQUEST_URI} show_table.php
RewriteCond %{QUERY_STRING} ^table=([0-9]{1,2})$
RewriteRule ^show_table\.php$ http://example.com/table/%1? [L,R=301,NC]
But when I visit example.com/show_table.php?table=12, I receive just redir-loop. I don't understant - the first is rewrite, the second is redirection, there ain't no two redirections. Do You see any error?
Thanks!
Instead of checking REQUEST_URI in the condition, you need to be checking in THE_REQUEST (which contains the full original HTTP request, like GET /show_table.php HTTP/1.1). When Apache performs the rewrite, it changes REQUEST_URI, so to the rewritten value, and that sends you into a loop.
# Match show_table.php in the input request
RewriteCond %{THE_REQUEST} /show_table\.php
RewriteCond %{QUERY_STRING} ^table=([0-9]{1,2})$
# Do a full redirection to the new URL
RewriteRule ^show_table\.php$ http://example.com/table/%1? [L,R=301,NC]
# Then apply the internal rewrite as you already have working
RewriteRule ^table/([0-9]{1,2})$ show_table.php?table=$1 [L,NC]
You could get more specific in the %{THE_REQUEST} condition, but it should be sufficient and not harmful to use show_table\.php as the expression.
You'll want to read over the notes on THE_REQUEST over at Apache's RewriteCond documentation.
Note: Technically, you can capture the query string in the same RewriteCond and reduce it to just one condition. This is a little shorter:
# THE_REQUEST will include the query string so you can get it here.
RewriteCond %{THE_REQUEST} /show_table\.php\?table=([0-9]{1,2})
RewriteRule ^show_table\.php$ http://example.com/table/%1? [L,R=301,NC]

.htaccess url rewriting and redirect 301

At the moment we have one page which shows a list of links
each link has got its own ID number
each link gets opened with the file info.php?ID=X
for example:
www.mysite.com/info.php?ID=1 shows the link "weather italy"
www.mysite.com/info.php?ID=2 shows the link "weather france"
Since we have several links for "weather italy" and "weather france" we would like to rewrite new urls (weather-italy and weather-france) in .htacces
Whith the new urls we would have the folowing structure:
www.mysite.com/weather-italy/info.php?ID=1
www.mysite.com/weather-france/info.php?ID=2
With the following code we tell the server to rewrite the urls and call the original file:
RewriteRule ^weather-italy/info.php?$ info.php [NC,L]
RewriteRule ^weather-france/info.php?$ info.php [NC,L]
This works fine.
To avoid double indexing we want to redirect 301 the old link to the new link.
We have achieved that with the following code:
RewriteCond %{THE_REQUEST} \?ID=1
RewriteRule ^info\.php$ http://www.touristinfo.fr/weather-italy/info\.php [L,R=301]
RewriteCond %{THE_REQUEST} \?ID=2
RewriteRule ^info\.php$ http://www.touristinfo.fr/weather-france/info\.php [L,R=301]
This also does the job but combined with the first part of the script is produces a never ending loop.
What is wrong with our code?
Thanks a lot for your help :)
%{THE_REQUEST} is supposed to only match if the url you want to match is an external request. Your problem is that the regex you made is not specific enough.
Let's examine what happens. You go to example.com/info.php?ID=2. The first two rules don't match, but the 4th one does. You end up with a redirect to example.com/weather-france/info.php?ID=2.
This goes through your .htaccess again. The second rule matches, and internally rewrites it to info.php?ID=2. The [L] flag doesn't make a difference here, because the url will be pulled through .htaccess until it stops changing. On the second cycle through .htaccess, the url will now match the 4th rule, even though the external request contained /weather-france/info.php?ID=2. ID=2 is in the external request too, and the internal rewrite is now info.php again.
The fix is to make %{THE_REQUEST} match enough so that the rewritten url doesn't match it anymore.
On a further note: Familiar yourself with the difference between regex and strings in RewriteRules and RewriteConds. You escaped a dot in a string, while leaving a dot in a regex unescaped. The ? is "match the previous character 0 or 1 times", not a question mark literal. The query string cannot be matched in the first argument of RewriteRule.
You'll end up with:
RewriteRule ^weather-italy/info\.php$ info.php [NC,L]
RewriteRule ^weather-france/info\.php$ info.php [NC,L]
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /info\.php\?ID=1
RewriteRule ^info\.php$ http://example.com/weather-italy/info.php [L,R]
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /info\.php\?ID=2
RewriteRule ^info\.php$ http://example.com/weather-france/info.php [L,R]

htaccess sport links redirects

Need to redirect set of hundred or so links from one domain to another. This is my current code (not working):
RewriteCond %{HTTP_HOST} ^www.onedomain.info/$1/staticword($2.*) [nc]
RewriteRule (.*) http://otherdomain.com/$1/staticword($2.*) [R=301,L]
Redirect domains themsleves is a no-brainer and that's correct I think, then I too think that $1 is correctly - cuz $1 is a variable for 12 different words for sport categories (like soccer or hockey), sometimes there is one word, sometimes the other (but ofc it should be the same, so this is why I have that $1 there - correct me if I am wrong but this could work I think...).
Problem is that after that there is one static word which is not changing (is same all the time in every link - it's something like "watch"...) BUT after that word there can be absolutely ANYTHING which I tried to solve by ($2.*) but it's wrong for some reason.
Can you help please? Thanks!
RewriteEngine On
# HTTP_HOST if to match domain names only
RewriteCond %{HTTP_HOST} ^www\.onedomain\.info$ [NC]
# On the left hand we match the rest and on the right we redirect
RewriteRule ^(.*)/(staticword.*)/?$ http://otherdomain.com/$1/$2 [R=302,L]
Note that I am using 302, because you want to test it first before change it to 301 so your browser does not get cached with it until you are sure it's working as you want it to.
So given your example http://onedomain.info/soccer/watchfe27789-mexico-vs-trinidad-and-tobago-gold-cu‌​p, the rule would be like this:
RewriteEngine On
# HTTP_HOST if to match domain names only
RewriteCond %{HTTP_HOST} ^www\.onedomain\.info$ [NC]
# On the left hand we match the rest and on the right we redirect
RewriteRule ^(.*)/(watchfe27789.*)/?$ http://otherdomain.com/$1/$2 [R=302,L]

.htaccess rewrite rule handled correctly

I want to redirect rewrite /zp-core/admin.php to admin but somehow I can't get it to work.
I've added this rule to my htaccess:
RewriteEngine On
RewriteBase /
RewriteRule ^/admin$ /zp-core/admin.php [L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^albums/?(.+/?)?$ $1 [R=301,L]
...
# Catch-all - everything else gets handled in PHP for compatibility.
RewriteRule ^(.*)/?$ index.php?album=$1 [L,QSA]
The third rule should do it, but somehow it doesn't. In the end it falls back on the catch all rule
When you say "rewrite A to B", usually, "A" is what the user types and "B" is where you want him to get.
If A='/admin' and B='/zp-core/admin.php' then the first rule is ok, if you remove other rules, it will work.
The problem is - apache looks into .htaccess on every request, including sub-requests, and your redirect does a sub-request... So it does not match the first rule anymore, but then comes the catch-all rule, and it is a match again (you'll see that $_GET["album"] will be /zp-core/admin.php when hitting index.php on such request.
What you need to do is to add RewriteCond either checking that the uri is not /zp-core/admin.php OR that you're not in sub-request...
Either:
RewriteCond %{REQUEST_URI} !^/zp-core/admin.php$
OR:
RewriteCond %{IS_SUBREQ} !=true

htaccess ReWriteCond ReWriteRule - redirect occurs even when URL includes nonsense characters

We've just finished a major re-structuring our website and I'm trying to write a set of redirect rules of varying specificity. The redirects are half working:
They correctly re-route old URLs
They incorrectly also allow and re-route URLs that include text not specified in the
ReWriteCond statements (when instead I would expect to see a "Not Found" error message displayed in the browser.)
Statements in the .htaccess file (located in the root of the web site) include:
RewriteBase /
RewriteCond %{REQUEST_URI} /company/company-history.html
RewriteRule (.*)$ http://www.technofrolics.com/about/index.html
RewriteCond %{REQUEST_URI} /press
RewriteRule (.*)$ http://www.technofrolics.com/gallery/index.html
The above correctly executes the desired redirect
but also works when I enter the following after the domain name:
/youcanenteranytext/hereatall/anditstillworks/press
In other words, any text following the domain and preceding the conditional string seems to be allowed/ignored. Any advise on how to restrict the condition or rewrite rule to prevent this would be much appreciated!
Thanks, Margarita
You need to including bounds in your regular expressions when you try to match against %{REQUEST_URI}, the ^ indicates the beginning of the match.
RewriteCond %{REQUEST_URI} ^/company/company-history\.html
Will make it so requests for /garbage/stuff/comapny/company-history.html won't match. And likewise:
RewriteCond %{REQUEST_URI} ^/press
Will make it so requests for /youcanenteranytext/hereatall/anditstillworks/press won't match. You can additionally employ the $ in your regular expression to indicate the end of the match, so something like this:
RewriteCond %{REQUEST_URI} ^/press$
Will ONLY match requests for /press and not /something/press or /press/somethingelse or /press/.

Resources