mod rewrite rule for parameters - mod-rewrite

I have below url(s)
www.localhost.com/profile.php?username=first.last
i would like to permanently redirect above url to using .htaaccess file. (apache server)
www.localhost.com/first.last
please also consider there are few other urls there but i dont want to touch them..like
www.localhost.com/message.php?id=12
www.localhost.com/editprofile.php?editname=first.last
www.localhost.com/uploadphoto.php?username=first.last
can anyone please help me.
thank you in advance.

You could try to handle the Query String with RewriteCond and pass the captured match to RewriteRule. You must exclude any .phpscripts of your rewriting rule otherwise it will create some problems with others URLs.
Don't forget to add the [QSA] tag after your RewriteRule otherwise it will not add the Query String parameters.
Maybe doing something like this:
RewriteEngine on
#serve any existing php scripts as usual
RewriteRule ^([a-zA-Z0-9]+\.php) - [L]
#and now handle your specific stuff:
RewriteCond %{QUERY_STRING} ^([a-zA-Z0-9]+\./[a-zA-Z0-9]+)$
RewriteRule ^(%1)$ /profile.php?username=%1 [QSA]
I don't test it but it should be a good beginning. You can read some good stuff here and inside the docs for mod_rewrite httpd 2.2 about how to write and handle specific rewriting use cases.

Related

How to remove a parameter from query string and rewrite URL?

I have a URL that appears like this
http://www.domain.com/previous-winners/?ceremony=406&title=2015
and i'm trying to rewrite this with my .htaccess file to appear like this. Keeping the title parameter and dropping the ceremony one.
http://www.domain.com/previous-winners/2015
This is what i have so far
RewriteRule ^previous-winners/$2 /previous-winners/?ceremony=$1&title=$2 [NC]
But i'm not really sure where to go next.
Use this rule in your .htaccess:
RewriteEngine On
RewriteRule ^previous-winners/([^/]*)$ /previous-winners/?ceremony=406&title=$1 [L]
It will leave you with the URL: http://www.domain.com/previous-winners/2015
Just make sure you clear your cache before you test this.

301 redirect with RewriteRules

I have a problem concerning RewriteRules.
I'd like to move one page permanently, so I want to use a 301 redirect. I tried this:
RewriteRule ^page1/([A-Z].*)$ http://www.abs.nl/page1/vraag-$1 [R=301]
However this does not work. Can someone please tell me how I could fix this? I already tried for hours to find an answer.
Should I also use a %{HTTP_HOST} condition? I see this a lot but I don't know how it works.
A couple things:
Make sure that you have RewriteEngine On before your rewrite rule.
Your current regex is looking for a capital letter followed by any character, is this what you want?
Try this:
RewriteEngine On
RewriteRule ^page/(.*)$ http://www.abs.nl/page1/vraag-$1 [R=301,L]
That redirect will match on any string of characters following page/ in a URL.

using mod_rewrite to point at a blog

I'm trying to use mod_rewrite to point the blog portion of a site to a blog site.
this is what I have to handle the normal stuff
RewriteRule ^(\w+)/?$ index.php?page=$1
This is what i'm trying to use for the blog site
RewriteRule ^blog/?$ http://url.to.my.blogger.site
but it's not working, when I go to site/blog it directs me to index.php?page=blog is there something I need to do to not do the second rewrite if the first is correct? like an if/else? sorry don't know much about mod_rewrite so any advice would be awesome.
also I noticed that if I try to do something like site/home everything works fine but if I attempt to hit site/home/ it puts all of my urls into the wrong context, for example my css and images don't get loaded correctly.
my full file is this
RewriteEngine on
RewriteRule ^blog/?$ remote/blog/uri/here
RewriteRule ^(\w+)/?$ index.php?page=$1
RewriteCond %{THE_REQUEST} index\.php
RewriteRule ^index\.php - [F]
and when i hit site/blog it still tries to serve index.php?page=blog, I'm guessing I have to break out of the code at some point? I couldn't find documentation on if/else statements
I needed to add flags to my RewriteRule lines so that the server wouldn't evaluate further. Changing them to be
RewriteRule ^/blog http://url.to.blog [L]
did the trick, the problem was that it was evaluating all the way down, seeing as I wasn't attempting to go to index the last valid rule to evaluate was the general rewrite rule.

Rewrite just part of url

I've got this type of urls on my site (as you can see dashes usage is quite random):
http://www.example.com/my_sub_directory/this--is--the-page-title---excellent-title
http://www.example.com/my_sub_directory/this-is--the--page-title--excellent-title
http://www.example.com/my_sub_directory/this-is-the-page----title---excellent---title
And I would like to rewrite them in the following format
http://www.example.com/my-sub-directory/this-is-the-page-title-excellent-title
As you can see the numbers of dashes in the original url is variable. Is this possible? Can I do it for all urls or can I just do it globally?
Can you please also provide a simple example on how to rewrite this
http://www.example.com/my_sub_directory/
into this
http://www.example.com/my-sub-directory/
Many thanks
Is this possible? Can I do it for all urls or can I just do it globally?
Yes, but not with an .htaccess rule. You could use a RewriteMap prg: if you have root access to the system or vhost config. However what you can do is some think like this:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*?--.*)$ remapper.php?url=$1 [L]
Then use the remapper script to munge the url parameter and issue a header("Location: ...") to do a 302 redirect to the "normalised" URI.
Re: the my_sub_directory/ rewrite, this is trivial for a fixed directory string, but the general case would need to be handled as above:
RewriteRule my_sub_directory/ my-sub-directory/ [L]

How do I make an exception to a mod rewrite rule?

I absolutely do not understand mod rewrite or the syntax to make it work. I have however managed to cobble together the following, which works exactly as I need it to on my site:
RewriteRule ^page/([^/\.]+)/?$ page.php?page=$1 [L]
it changes www.mysite.com/one into www.mysite.com/page.php?page=one
wonderful.
However, is there a way that I can add an exception to the rule, so that if I try to use the url www.mysite.com/feed for example, it will go to www.mysite.com/feed/index.php rather than trying to redirect to www.mysite.com/page.php?page=feed
Thanks in advance!!
There are a couple of ways you could do this. Probably best would be to add a RewriteCond above the RewriteRule. Something like this (untested):
RewriteCond !^feed
RewriteRule ^page/([^/\.]+)/?$ page.php?page=$1 [L]
If you can't get that to work then you could also put a different RewriteRule above your one to redirect before this rule is matched...

Resources