mod_rewrite doesn't work both directions - apache

I get an original url:
www.mydomain.com/menu/?myid=29&mypage=pizza-hut.html
with the following mod_rewrite code:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^menu/([^/]*)/([^/]*)\.html$ /menu/?pid=$1&alias=$2 [L]
I get this nice url:
www.mydomain.com/menu/29/pizza-hut.html
so, both url above reference the same page...right!
now the real deal is,
WHY when I type the url,the original one:
www.mydomain.com/menu/?myid=29&mypage=pizza-hut.html
it doesn't redirect to
www.mydomain.com/menu/29/pizza-hut.html
it keeps its original one in the address bar, is there any line I should add?

Essentially, all the links you generate should be the nice urls. Don't have links like /menu/?myid=29&mypage=pizza-hut.html in any of your pages. Use the clean URLs that you've ensured to route correctly on the back end that look like this: www.mydomain.com/menu/29/pizza-hut.html
The rules that you have rewrite on the server the nice looking urls to what your content understands (e.g. /menu/?myid=29&mypage=pizza-hut.html). That's the most important part. If you want to correct all the direct requests for the ugly URLs, you need to first make sure all your pages start using the nice looking ones, then you can maybe do something like this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /menu/?\?myid=([^&]+)&mypage=([^&\ ]+)
RewriteRule ^menu/?$ /menu/%1/%2? [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /menu/?\?myid=([^&]+)&mypage=([^&\ ]+)
RewriteRule ^menu/?$ /menu/%1/%2.\html? [R=301,L]
Note the ? following the .\html

Related

Redirecting Dynamic Url to Static Url

I have read a lot of suggestion, article but i could not fixed my problem. Here is my Url :
us/my-blog?catid=1&id=404:my-thought-of-the-day-problem-of-traditional-education
ja/resources?id=366:video-clip
I want to redirect this URL to static URL. I have tried with .htaccess Like this :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI}
(/|.php|.html|.htm|.feed|.pdf|.raw|/[^.]*)$ [NC]
RewriteRule (.*) index.php
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
Redirect 301
/tw/en/investment-opportunities/ultimate-banking-how-to-create-your-own-bank
http://takeshiyashima.com/tw/investment-opportunities/ultimate-banking-how-to-create-your-own-bank
Above URL Fixed and working now but the top URL i have mentioned are not working. I need help.
Please, give a more clear example and post the right link, as it is not clear if there is any symbol before 'ja/resources' and if it's a part of the link at all.
As I understand you want to take only the part:
'my-thought-of-the-day-problem-of-traditional-education'
and redirect to: 'http://takeshiyashima.com/tw/investment-opportunities/my-thought-of-the-day-problem-of-traditional-education'
If it's true here is the possible rule:
us/my-blog?catid=1&id=404:my-thought-of-the-day-problem-of-traditional-education
RewriteRule ^\/?us/my-blog\?catid=\d+id=\d+\:(.*) http://takeshiyashima.com/tw/investment-opportunities/$1 [R=301,L]
Not tested and can be made shorter. If you give more details to the example I will try to help.
Update according to your comment
For ja/resources?id=366:video-clip to takeshiyashima.com/ja/resources it is:
RewriteRule ^\/?ja/resources.* http://takeshiyashima.com/ja/resources [R=301,L]
For 'us/my-blog?catid=1&id=404:my-thought-of-the-day-problem-of-traditional-educatio‌​n', everything is clear, but I see no logic in '/1-my-thought', we have to have a clear pattern. If you say that
you want to take always first 2 words from 'my-thought-of-the-day-problem-of-traditional-educatio‌​n'
then we can make '/1-my-thought', but if you will have different strings like 'my-stuff-education-is-cool', then we will not be able to apply a pattern using regular expressions. Please, provide more original links (3+) you would like to change, so I can see if there is any pattern.
If there is no pattern at all it is possible to use, for example, RewriteMap for Apache with an additional script on the server side, which will handle your links, but it's a more complex way, so let's stick to regular rewrites for now. Waiting for 3+ more examples.

Redirecting to same page with .htaccess

From my .htaccess file:
RewriteRule ^showPAGE.php page [NC,R=301]
RewriteRule ^page showPAGE.php [NC,L]
I want users going to url domain.com/showPAGE.php to be redirected to domain.com/page .
When domain.com/page is being entered, I want it to show the content of the file showPAGE.php.
Is that possible to do?
The above results an infinite redirection loop.
Thanks
You're trying to do something that's very tricky. The problem is that, by design, the RedirectRule directive always triggers again the complete set of rules. You can only get out of the loop when you obtain a final URL that does not match any of the rules and that's the tricky part since you are reusing the showPAGE.php name.
My best attempt so far involves adding a fake hidden string:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/showPAGE\.php
RewriteCond %{QUERY_STRING} !^internal
RewriteRule ^ http://%{HTTP_HOST}/page [NC,R=301,L]
RewriteRule ^page$ showPAGE.php?internal [NC,L]
It works but it's not pleasant. Definitively, it's easier to handle the redirection from with PHP or to simply pick another name.
The redirect from showPAGE.php to page needs to have [L] so that it will stop processing and redirect at once, rather than going on and applying other rules (which at once map it back to showPAGE.php). Try this:
RewriteRule ^showPAGE.php page [NC,R=301,L]
RewriteRule ^page showPAGE.php [NC,L]

How do I make .htaccess do what I want? :) (appending query string to url)

Currently my .htaccess looks like this...
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]
It currently changes any /xxx.php file into /xxx. This is great for SEO. However, I also want Mr. htaccess to convert certain URLs into a URL + query string. For instance when user goes to
/specific/somerandominfo
Then somerandominfo is passed to the specific.php file. I normally have no problem doing this using rewrites, but because of my fancy catchall rewrite, I can't figure out how to do it.
For example if I add
RewriteRule ^specific/([^/]+)$ /specific.php?somerandominfo=$1 [NC]
to my .htaccess, then hitting up /specific/somerandominfo just serves me a big fat 500 Internal Service Error.
Any help from you apache gurus out there would be so, so cool.
Thanks!
p.s. anybody want to also throw in any other cool SEO tricks that they like? I'll bake you cookies.
You are getting 500 error because your rules are creating an infinite cycle. Check apache error log to see if it is true. So you should design your rules properly. Maybe like that:
RewriteRule ^([^/]*)$ $1.php [L]
RewriteRule ^(.*)/(.*)$ $1.php?var=$2 [L]
RewriteRule ^specific/([^/]+)$ /specific.php?somerandominfo=$1 [NC]
This is mostly correct. I'd just add the B flag, like this:
RewriteRule ^specific/([^/]+)$ /specific.php?somerandominfo=$1 [NC,B]
This causes the capture group $1 to be properly escaped for use in query strings. Note that you can still use QSA to retain the query parameters used in the original request (in addition to somerandominfo).
Perhaps you'll want to post your actual RewriteRule.

Beginner's apache mod_rewrite assistance

I am not really familiar with apache mod_rewrite.
I have url parameters such as {domain}/index.php?blog=5
I simply want to make it {domain}/home.php?client=5
Is it a task as simple as it sounds and can anyone help?
The following might work, give it a try
RewriteCond %{REQUEST_URI} ^/home.php [NC]
RewriteCond %{QUERY_STRING} client=([0-9]+) [NC]
RewriteRule (.*) http://%{REMOTE_HOST}/index.php?blog=%1 [L]
That seems pretty simple, to be honest — once you get your head into mod_rewrite, it's not that complex.
It sounds like you want to add
RewriteEngine on
RewriteRule ^/index.php?blog=(.+)$ /home.php?client=$1
to your configuration.
Some caveats:
If you are putting this in a .htaccess file, then remove the / from the RewriteRule line.
If you want to make this case-insensitive, add [NC] to the end of that same line.
If you want users to see the URL change (so sending a 302 Found redirection to the browser), then add [R] to the end of the RewriteRule line.
If you want both a 302 Found and for the URL to be case-sensitive, combine the two instructions as [NC,R] at the end of the RewriteRule line.
It's definitely worth reading the mod_rewrite docs, but the rule above should be all you need for this use-case.

.htaccess rewrite rule for redirects

I've been fighting this for a while and can't seem to make it work. My old system used a lot of query strings to render pages but they are no longer necessary. My url is below:
OLD URL: www.example.com/links.php?section=5&catid=52
NEW URL: www.example.com/mhfs/links
The name links is coincidental and not necessarily from the old pages name. I need to check which section and catid is present and redirect them to the appropriate page from what it is. I tried the following but this just seems to do nothing. What am I doing wrong?
RewriteCond %{REQUEST_URI} ^links.php$
RewriteCond %{QUERY_STRING} ^section=5$
RewriteCond %{QUERY_STRING} ^catid=52$
RewriteRule ^(.*)$ /mhfs/links? [R=301]
Any help would be GREATLY appreciated.
You must turn the rewrite engine on for it to work.
You probably don't want the regex start symbol on the replacement:
RewriteEngine on
RewriteCond ...
RewriteRule ...