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]
Related
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.
I have a number of URIs that read as follows;
http://www.domain.com/index.php?route=information/information&information_id=2 (about)
http://www.domain.com/index.php?route=information/information&information_id=4 (help)
http://www.domain.com/index.php?route=information/information&information_id=7 (contact)
....
Is there a way I could avoid writing explicit but similar rewrite rules?
EXAMPLE
RewriteRule ^contact$ index.php?route=information/information&information_id=2 [NC,L] # About
RewriteRule ^help$ index.php?route=information/information&information_id=4 [NC,L] # Help
Lots of possibilities. Easiest might be something like this:
^(.*)$ index.php?route=information/information&information_id=$1
Then handle the information_id string in your index.php. Make sure it only allows acceptable values and 404s everything else.
Or to be more restrictive at the mod_rewrite level:
^(contact|help|foo)$ index.php?route=information/information&information_id=$1
If you have more than a handful of pages to rewrite, the key is a to have a single, simple rewrite rule combined with logic in your index page to serve the correct content based on the URL requested.
Take a look at the .htaccess and index.php for any popular CMS which supports url rewriting and you will find what you seek.
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.
for a site move I need to hardcode some very simple URL Rewrites. I only see examples with regular expressions but what I need is this:
I have a CSV like
new URL, old URL
http://shop.example.com/categoryB, http://example.com/shop/categoryC
as you see, as the category names are not the same, there is no Regex magic necessary, I just want to create a .htaccess file with one URL Rewrite per line, that's it.
I tried
RewriteRule http://shop.example.com/categoryB http://example.com/shop/categoryC [L,R=301]
but this doesn't seem to work,
thanks for the help!
Have you turned on the ReWrite engine at the start of your htaccess file?
I would guess at something like this:
RewriteEngine On
RedirectMatch 301 http://shop.example.com/categoryB http://example.com/shop/categoryC
Ok, the answer looks like this:
RedirectMatch 301 /categoryOLDSITE http://example.com/shop/categoryNEWSITE
important is the slash in front of the old site and that it isn't a complete URL but a relative one to the location of the .htaccess file.
further information here: http://httpd.apache.org/docs/2.0/mod/mod_alias.html
how would I rewrite in .htaccess:
url.com/NAME to url.com/view.php?=NAME
I've tried a couple of variations but I think i might be putting a slash in the wrong place. Any suggestions?
RewriteEngine on
RewriteRule ^(\w+)$ /view.php?somevar=$1 [L]
You have to set NAME as the value of some variable.
This example allows NAME to be any word.. if you want something specific... just replace \w+ with whatever specific thing you want.