Have been trying to change my urls to make them more SEO friendly
and at the moment have managed to get them sort of working how
I want it.
My original url is this:
http://www.example.com/index.php?keyword=nikon
So managed to get this rewriting so that it now looks like this:
http://www.example.com/compare/nikon.html
Using this:
RewriteEngine On
RewriteRule ^compare/([^/]*)\.html$ /index.php?keyword=$1 [R,NC,L]
But I have tried to get the end result so that the url in the address bar
is like this: http://www.example.com/compare/nikon.html
If I change it so that the rewritten url is in the address bar will the
variables still be passed and in instances where there is an anchor # tag
after the url will it still work?
Thanks for any help
You want to get rid of the R flag in your rule. That's causing the address bar to be changed to the original ugly looking URL.
Additionally, if you want to redirect the browser from the ugly to the nicer looking URL, you need a different rule. So something like:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+index\.php\?keyword=([^&\ ]+)
RewriteRule ^ /compare/%1.html? [L,R]
RewriteRule ^compare/([^/]*)\.html$ /index.php?keyword=$1 [NC,L]
Related
I'm working on a website and now i want to edit the htaccess file to change a url with get variable. But i look on the internet for information without any success.
http://xxxxx.domain/massage.php?massage={massageName}
this is the link and i want it to look like this
http://xxxxx.domain/massage/{massageName}
Can someone help me with that??
Martijn
Try:
Options -Multiviews
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+massage\.php\?massage=([^&\ ]+)
RewriteRule ^ /massage/%1? [L,R]
RewriteRule ^massage/(.+)$ /massage.php?massage=$1 [L,QSA]
Make sure to turn off Multiviews, which is a mod_negotiation feature that will pre-emptively affect the request before mod_rewrite can run.
You need a rule to redirect the browser from the query string URL to the nicer looking URL, then the browser will send a second request for the nicer looking URL
You then need a rule to internally rewrite the nicer looking URL to the one with the query string. This happens entirely on the server's end.
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]
I have a site which has a url structure like so:
url/example1/sometext
So I dont have to go through and change a crazy amount of code, can I use htaccess to do the following?
url/newword/sometext redirects to: url/example1/sometext with url/newword/sometext still shown the address bar.
I have tried some rewrites like:
RewriteRule ^(.*)example1(.*)$ url/$1newword$2 [R=301,L]
Any ideas?
As long as the "url" part is still the same, then you can just get rid of the R=301 flag:
RewriteRule ^(.*)example1(.*)$ /$1newword$2 [L]
I recently with the help of SO solved my htaccess rewriterule issue:
Reuse subdomain in a rewrite rule
The result is that when somebody enters
whatever.example.com/anypage
in the adress bar, the htaccess automatically redirects to
whatever.example.com/somepath/whatever/anypage
What I wish to do is to find a way to just show whatever.example.com/anypage in the adress bar with the content of whatever.example.com/somepath/whatever/anypage displayed.
In the post I mentioned earlier, Jon Lin clearly mentions the following:
redirects always change what's in the browser's URL address bar
However I know some very frequent cases of url rewritting that would show in the adress bar let's say, for instance:
example.com/article-1-15
but actually showing the content of
example.com/somepath/somepage.php?article=1&otherparam=15
How could this apply to my case? I really wish to have a tiny url but it seems I missed something.
You may try something like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/([^/]+)$
RewriteRule .* http://whatever.example.com/somepath/whatever/%1 [L]
It will map:
http://whatever.example.com/anypage
To a resource at:
http://whatever.example.com/somepath/whatever/anypage
showing always in the browser's address bar:
http://whatever.example.com/anypage
UPDATED
If whatever is dynamic and is the same in the substitution URI, here is another option:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ([^/]+)\.example\.com.*
RewriteCond %{REQUEST_URI} ^/([^/]+)$
RewriteRule .* http://%1.example.com/somepath/%1/%2 [L]
This will work as long as both "whatever" in the substitution path are the same. If they are not, the last "whatever" has to be hardcoded, like this:
RewriteRule .* http://%1.example.com/somepath/whatever/%2 [L]
There is no other way as the incoming URL doesn't have it.
Please help, how to make this url
(in my request i use get function)
iplookup.php?lookup_ip=66.249.66.1
to looks like this?
/ip/66.249.66.1
with
RewriteCond %{QUERY_STRING} lookup_ip=
RewriteRule ^ip/(.*)$ iplookup.php?lookup_ip=$1 [QSA]
But unsuccessful :(
Get rid of the condition. The RewriteCond %{QUERY_STRING} lookup_ip= condition is only true if lookup_ip= is in the query string, and obviously the URI /ip/66.249.66.1 doesn't have one. Your rule should just look like this:
RewriteRule ^ip/(.*)$ /iplookup.php?lookup_ip=$1 [L,QSA]
Then when you request http://yourdomain.com/ip/12.34.56.78, the browser's URL address bar remains unchanged while you get served the content at /iplookup.php?lookup_ip=12.34.56.78. You just need to make sure all of your links look like http://yourdomain.com/ip/12.34.56.78.