htaccess clean URL's what's the best way to do it? - .htaccess

I'm working on clean Url's for my website and I noticed that what you find on the internet is pretty much about rewriting your clean urls to the url that your server can work with. So something like this:
www.domain.com/profile/username --> www.domain.com/profile.php?u=username
RewriteEngine On
RewriteRule ^profile/(.*)$ /profile.php?u=$1 [L]
So now with this I should link within my website to the cleanurl's. But intuitively it feels more solid/better to link to the 'dirty' url. So in that case you should use rewriterules for the above AND a redirect rule to rederict the 'dirty url' to the clean url, so the user only sees the clean url in the browser at all time.
www.domain.com/profile.php?u=username --> www.domain.com/profile/username
Is this something that is a good thing to do. Is it done by websites? What are arguments against/ in favour of it?
And how do you do you do a redirect like this. Something like the following doesn't seem to work:
RewriteRule ^profile\.php?u=(.*)$ /profile/$1 [R=301, L]
I'm fairly new to .htaccess so I might ask for something that's obvious...

RewriteRule ^profile\.php?u=(.*)$ /profile/$1 [R=301, L]
Isn't going to work because you can't match against the query string from a RewriteRule (you also have a stray space in your flags). You also want to be matching against the actual request and not the URI because you'd just cause a redirect loop from your other rule.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /profile\.php\?u=([^&\ ]+)
RewriteRule ^/?profile\.php$ /profile/%1 [R=301,L]

Related

htaccess - rewrite rule - redirect after url rewrite

This is pretty simple, but can't seem to figure this out.
I have a url /shows/index.php?id=1 that I wanted to appear in the url as /shows/1/index.php.
So I added this to my htaccess:
RewriteRule ^shows/([0-9]*)/index.php$ /shows/index.php?id=$1 [L]
This worked like a charm. I can now navigate to /shows/1/index.php no problem at all. However, I can also still navigate to /shows/index.php?id=1. I wanted that page to automatically redirect to the new url, so I wrote this:
RewriteRule ^shows/index.php?id=([0-9]*)$ /shows/$1/index.php [R=301,L]
...but it doesn't do anything. However, if I do something like:
RewriteRule ^shows/([0-9]*)/0$ /shows/$1/index.php [R=301,L]
It redirects just fine. So that makes me think there is an issue with the first part of the rewrite rule, maybe? I'm not too familiar with this sort of stuff.
Since you want to redirect and rewrite the same request, you need to match against %{THE_REQUEST} variable otherwise you will get an infinite loop error on the redirect
RewriteEngine on
# redirect /shows/index.php?id=1 to /shows/1/index.php
RewriteCond %{THE_REQUEST} /shows/index.php\?id=([^\s]+) [NC]
RewriteRule ^shows/index.php$ /shows/%1/index.php? [NC,L,R]
#rewrite /shows/1/index.php to /shows/index.php?id=1
RewriteRule ^shows/([0-9]*)/index.php$ /shows/index.php?id=$1 [L]
Clear your browser cache before testing this.

htaccess 301 redirect of single URL to new path fails

I have a couple of structurally unrelated links that I need to forward. Since they are different I wont be able to use a common pattern but have to forward each manually. They look something like this:
shop/single/?products[backPID]=127&tt_products[sword]=yoo&
tt_products[product]=822&cHash=8758b181408d3380euios61
I tried forwarding with something like this but it doesnt work while other "normal" links work just fine...
Redirect permanent /shop/single/?products[backPID]=127&tt_products[sword]=yoo&tt_products[product]=822&cHash=8758b181408d3380euios61 http://www.new-url.de/shop/
Also tried but unfortunately not working:
RewriteRule ^shop/single/?products[backPID]=127&tt_products[sword]=yoo&tt_products[product]=822&cHash=8758b181408d3380euios61 http://www.new-url.de/shop/ [QSA,R=301]
Any clues to why this might be? Do I have to do something with the parameters before theys can be forwarded?
Thanks
You need to use a RewriteCond to match against the full uri
RewriteEngine on
RewriteCond %{THE_REQUEST} /shop/single/\?products\[backPID\]=127&tt_products\[sword\]=yoo&tt_products\[product\]=822&cHash=8758b181408d3380euios61 [NC]
RewriteRule ^ http://www.new-url.de/shop/? [R,L]

Remove first part of url

I am quite sure that this is the right way, but it's not working. I'm trying to rewrite this
domain.com/halloo/wp-content/uploads/image.jpg
to
domain.com/wp-content/uploads/image.jpg
using this in the .htaccess
RewriteRule ^/halloo/wp-content/(.*)$ /wp-content/$1 [R=301,L]
I can't figure out why it's not working.
The regex targets for rewrite rules in htaccess files won't start with a /, which means your rule will never match (because there's never a request that starts with /).
Also, your rule takes the request /halloo/wp-content/foo and redirects the browser to /wp-content/foo. If you want to rewrite it internally so that /halloo/wp-content/foo remains in the URL address bar, remove the R=301, part from the flags.
RewriteRule ^halloo/wp-content/(.*)$ /wp-content/$1 [L]
This means you must request domain.com/halloo/wp-content/uploads/image.jpg in the browser. If you actually wanted it the other way around, just swap the "from regex" and the "to URI":
RewriteRule ^wp-content/(.*)$ /halloo/wp-content/$1 [L]
Edit:
To get rid of the /halloo/ from the browser's address bar, you need something like this:
RewriteCond %{THE_REQUEST} \ /+halloo/wp-content([^ \?]+)
RewriteRule ^ /wp-content$%1 [L,R]

htacces - need to fix broken links coming from other sites to mine

I am having an issue where Google Webmaster Tools is reporting a ton of 404 links to my site which are coming from ask.com.
I have tried to get ask.com to fix their side but of course they are not, so now I am stuck with over 11k of bad links to my site which I am suspecting is effecting my ranks right now.
Anyways I have a possible way to 301 them, but not sure how to do it with .htaccess.
Here is the bad link pointing to my site
http://www.freescrabbledictionary.com/sentence-examples/fere-film/feverous/about.php
It should be
http://www.freescrabbledictionary.com/sentence-examples/fere-film/feverous/
Besides the about.php there are other variations of endings as well, I basically need to be able to remove the ending.
Problem is that the URL after /sentence-examples/ can change. The beginning is always:
http://www.freescrabbledictionary.com/sentence-examples/
So basically:
http://www.freescrabbledictionary.com/sentence-examples/<-keep but can change->/<-keep but can change->/<-remove this->
This .htaccess should be placed on the folder before sentence-examples:
RewriteEngine on
# Redirect /sentence-examples/anything/anything/remove to /sentence-examples/anything/anything/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(sentence-examples/[^/]+/[^/]+)/.* [NC]
RewriteRule ^ /%1/? [R=302,PT,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(.*)$ /sentence-examples/examplesentence.php?havethis=$1&word=$2 [L]
Change 302 to 301 once you confirm it's working as expected.
If you have a CMS installed you might need a different rule to work along with it without conflicting.
Keep in mind that if you had previously tried different redirects using 301 aka permanent redirect its recommended that you use a different browser to test this rule to avoid the caching.
This is possibly quick and dirty but I've done a simple test on localhost and here just to make sure it works.
RewriteEngine On
RewriteRule ^sentence-examples/(.*)/(.*)/(.*)\.php http://www.freescrabbledictionary.com/sentence-examples/$1/$2/ [R=301,L]
You can see that I've added wildcard groups (.*) to the RewriteRule so that we can pick up the elements of the URL that we need to aid in proper redirection i.e. $1 and $2. You can also use the third one ($3) to get which destinations are being targeted alot for your SEO needs.
NB: The rule above assumes that that the redirected URL will always be from a .php target and to ensure that you can redirect regardless of whatever comes after the 3rd URL segment replace the RewriteRule with this
RewriteRule ^sentence-examples/(.*)/(.*)/(.*)$ http://www.freescrabbledictionary.com/sentence-examples/$1/$2/ [R=301,L]

Using mod_rewrite to mask a directory/file name in a URL

I've taken my site down for some prolonged maintenance and am using mod_rewrite to send all requests to a single page: www.mysite.com/temp/503.php
This is my .htaccess file which works fine.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/temp/503.php [NC]
RewriteRule .* /temp/503.php [R,L]
However, what I'd also like to be able to do is to hide /temp/503.php in the resulting URL from the visitor.
I know this is perhaps trivial and I'm sure fairly simple to achieve, but with my limited mod_rewrite skills I can't seem to get it to work.
Any help would be much appreciated.
Thanks.
Just get rid of the R flag in the rewrite rule, which tells the rule to redirect the request, thus changing the URL in the browser's location bar. So the rule would look like:
RewriteRule .* /temp/503.php [L]
which internally rewrites the requested URI instead of externally telling the browser that it's been moved to a new URL.

Resources