Very Simple URL Rewrite withou Regex - url-rewriting

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

Related

How can I rewrite a URL, splitting the path into separate querystring arguments?

I want to rewrite an URL such as: mysite.com/Section/Subsection/SubSubSection into
mysite.com/index.php?s=Section&sub1=SubSection&sub2=SubSubSection
I wrote this rule:
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ /index.php?s=$1&sub1=$2&sub2=$3
But the problem is that Section, SubSection and SubSubSection are not mandatory at all, but if I insert some '?' character it goes on Internal Server Error...
(I tried this:
RewriteRule ^([^/]*)(/([^/]*))?(/([^/]*))?/?$ /index.php?s=$1&sub1=$3&sub2=$5
but it doesn't work)
Another problem is that there are other URLs that should not be edited, such as /res/img/logo.png.
The site in question is dynamic, so I don't know the section names beforehand. I know URLs that should be excluded, of course.
Any solutions? Thanks in advance.
I think you should do something like that ...
RewriteRule ^/([a-zA-Z]*)/([a-zA-Z]*)/([a-zA-Z]*)/?$ /index.php?s=$1&sub1=$2&sub2=$3
regards,
Mimiz

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]

PHP $_GET[] Mod Rewrite

I've made a website, and page links are formed like this: ?page=projects_project1.
I want the pages to be accessible this way as well: projects/project1. So ?page= should be removed, and _ should be replaced by /... So what's being accessed now using ?page=projects_project1, should become accessible at projects/project1...
What do I need to put in my .htaccess file to achieve this? Please also explain how it is done, so I can do it myself next time.
Try this out:
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^([^/]+)/([a-z0-9_]+)$ index.php?page=$1_$2 [NC,L]
The first line says that it should follow the symbolic links that may exist, the second line actually switches on the RewriteEngine and the third matches links that look like the following:
anything_except_a_forward_slash/anything_here_that_is_an_alphanumeric_or_forward_slash

Rewrite changing Url

I'd like to use apaches mod_rewrite to rewrite my url's. Normally, that i could do that, but i think is a special case. The content on the page depends on the url parameters, but is also capable of altering those parameters, and thus gaining new content without refreshing. The same parameters would always give the same result, however.
My url currently looks like this:
http://example.com/#?div1=1&div2=5
I would like to be able to use a neat, clean url, like http://example.com/nameofarticle. Does using mod_rewrite prevent changing a parameter that is "hidden" beneath such a url? And if not, how can i use mod_rewrite in order to allow me to keep working like this?
Thanks in advance!
I think you need the RewriteCond %{QUERY_STRING} before your RewriteRule. Something like:
RewriteCond %{QUERY_STRING} div1=(.*)&
Then you can access the bracketed values in the RewriteRule with %1 ... Just as you'd use $1 for bracketed values in the RewriteRule itself.
You may also want to be looking at the RewriteRule flag [QSA]. Quoting the mod_rewrite docs:
'qsappend|QSA' (query string append)
This flag forces the rewrite engine to
append a query string part of the
substitution string to the existing
string, instead of replacing it. Use
this when you want to add more data to
the query string via a rewrite rule.

mod_rewrite > /user/tag1/tag2/tag..?page=1

I am trying to use mod_rewrite to pretty up a URL.
I want the URL to look like this:
http://example.com/bart/school?page=2
and the rewritten URL to be:
http://localhost:8080/app?user=bart&tag1=school&page=2
If possible, I would also like to be able to have more than one tag per user:
http://example.com/bart/school/lisa?page=2
Would look like:
http://localhost:8080/app?user=bart&tag1=school&tag2=lisa&page=2
I far as I can tell this is possible by using mod_rewrite but I can't seem to figure it out. Any help would be really appreciated!
For the single tag use the following:
RewriteCond %{QUERY_STRING} ^page=([0-9]+)$
RewriteRule ^([^.]+)/([^.]+)$ http://localhost:8080/app?user=$1&tag1=$2&page=%1

Resources