Use .htaccess to rewrite URL with GET variables - .htaccess

I would like to make my url of:
example.com/index.php?this=a&that=b
to:
example.com/index.php/a/b
So that whenever somebody visits this page directly:
example.com/index.php/a/b
It will display in the address bar as typed, but will render the content for:
example.com/index.php?this=a&that=b
How is that done?

You can use that in your .htaccess:
RewriteEngine on
RewriteRule ^index\.php/([^/]+?)/(.+)/?$ index.php?this=$1&that=$2 [L,NC]

Related

htaccess rewriting address bar

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]

Replace / mask word in URL using htaccess

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]

rewriting url to hide real page path

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.

Replacing Segment with mod rewrite in .htacces

I'm trying to change this URL:
domain.com/?ACT=52&id=28
to this one:
domain.com/site.html
I want to replace the last segment ?ACT=52&id=28 to site.html. There are no parameters to consider. The URL domain.com/?ACT=52&id=28 will always be the same.
Try putting this in the htaccess file in your document root:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^ACT=52&id=28$
RewriteRule ^/?$ /site.html [L]
This makes it so when someone goes to domain.com/?ACT=52&id=28, they get served the content at domain.com/site.html, but if you to change the URL that's in the browser's URL address bar, you need to REDIRECT, not internally rewrite, so add the redirect flag in the square brackets: [L,R]

htaccess rule for redirection of any particular name to particular page

I am using PHP and I need to use a name say "Pranav dave" in url which should redirect me to "myprofile.php" what should i do?.. also the url should display me "Pranav dave".. and what should be the htaccess rule to redirect any html file to php file?
You could make it like this:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)$ myprofile.php?profile=$1 [NC]
After that, you can use your urls like this http://example.com/Pranav dave. The only problem is, that the browser will rewrite the url, and after that, its looking like followink one http://example.com/Pranav%20dave

Resources