Htaccess Rewrite Part of the url but keep the rest - .htaccess

Sorry but I just can not find the answer
I would like to change
http://www.domain.com/video.php?id=ALWAYS-A-NUMBER/ALWAYS-A-TITLE
to
http://www.domain.com/video/ALWAYS-A-NUMBER/ALWAYS-A-TITLE
So far I am using the code below but it also removes /ALWAYS-A-TITLE so I end up with "This-bit-is-ok/video/94015"
RewriteCond %{THE_REQUEST} \ /video.php\?id=([0-9]+)
RewriteRule ^ /video/%1? [L,R]
RewriteRule ^video/([0-9]+)$ /video.php?id=$1 [L]
Can anyone see why the end is missing please ?
Thanks

Try :
RewriteCond %{THE_REQUEST} \ /video.php\?id=([0-9]+)/([^\s]+) [NC]
RewriteRule ^ /video/%1/%2? [L,R]
RewriteRule ^video/([0-9]+)/([^/]+)/?$ /video.php?id=$1/$2 [L]

Related

Remove part from URL

I know that there is a lot of topic about this problem, but I can't solve the problem.
I want to remove /part/ from the URL.
Example:
FROM
www.mydomain.com/en/part/title/
TO
www.mydomain.com/en/title/
I managed to remove /part/ from the URL, but I become a 404 ERROR.
Could you please tell me what I am doing wrong?
Here my code:
RewriteEngine On
RewriteCond %{THE_REQUEST} //
RewriteRule ^.*$ /$0 [R=301,L,NE]
RewriteBase /
RewriteRule ^(([a-z0-9\-]+/)*[a-z0-9\-]+)$ $1/ [NC,R=301,L]
RewriteRule ^en/part/\d+-(.+) /en/$1 [R=301,L]
Thank you.
If you want to remove /part from the URL should it exist then you can use this rule:
RewriteRule ^(.*)/part(.*)$ $1$2

Redirect URL with GET variable to site with the GET variable in it

I would like to redirect, for example :
index.php?name=sarah
to
names/sarah.php
The folder structure:
names/...
index.php
I've tried :
RewriteRule index.php?name=$1 /names/$1.php
and other ones, but didn't came to a solution.
Is this possible? If so, how can you do this?
Note:
The other way round is
RewriteRule ^names/([^/]*)\.php$ index.php?q=$1 [QSA,L]
which works fine.
Here is your rule:
RewriteCond %{QUERY_STRING} name=([^&]*)
RewriteRule ^.*$ /names/%1.php?
You first example wont work as you are trying to manipulate query strings using a RewriteRule but this is not allowed. You can try the following :
RewriteEngine on
RewriteCond %{THE_REQUEST} / index\.php\?q=([^\s]+) [NC]
RewriteRule ^ /names/%1.php? [L,R]
RewriteRule ^names/([^/]*)\.php$ index.php?q=$1 [QSA,L]

What's wrong with this RewriteRule?

Here's my code in htaccess:
RewriteCond %{THE_REQUEST} \s/user/\?user=(.*)\s [NC]
RewriteRule ^ /user/%1? [R=301,L]
RewriteRule ^user/(.*)$ /user/?user=$1 [L]
What I want it to do is take the URL /user/?user=username and rewrite it to /user/username
What I have successfully rewrites it to /user/username but then it gives me a 500 error. If anyone could tell me why I would be very appreciative.
Thanks!
Edit:
The 500 error seems to be because it's creating a redirect loop with this rule. I'm not sure how to take just the last part and append it to the URL as a query string.
Please change this line :
RewriteRule ^user/(.*)$ /user/?user=$1 [L]
with this :
RewriteRule ^user/[a-zA-Z]+$ /user/?user=$1 [L]
You should match only letters , otherwise the above condition RewriteCond %{THE_REQUEST} \s/user/\?user=(.*)\s [NC] doesn't make any sense with (.*)$

Htaccess rewriterule and special characters '&'

I am trying to make some really simple redirection from an old to a new website. But all my redirection with the character '&' dont work.
For exemple:
RewriteRule ^-Nos-equipes-$ /-Associes-Collaborateurs-?lang=fr [R=301,L]
work fine, but:
RewriteRule ^-Contact-?&lang=en$ /-Nous-contacter-?lang=en [R=301,L]
Dont work.
I am sure its easy, but i dont find anyway to make it work. I have try flag [NE] , or write [&]...
Thanks. Casp.
EDIT:
I now use the solution anubhava who works;
but not totaly cause my problem is more complexe;
In fact i got some url with param:
?&lang=en, ?&lang=fr, and ?lang=en
So i have had other query string like that:
# Redirection from the old site #--------------------------------------
RewriteCond %{QUERY_STRING} (^|&)lang=fr(&|$)
RewriteCond %{QUERY_STRING} (^|&)lang=en(&|$)
RewriteCond %{QUERY_STRING} ^lang=en(&|$)
This work still great; except in 2 cases:
the first one:
trying to make this redirection (stay on same url but juste modify the param)
RewriteRule ^the_url?&lang=en$ /the_url?lang=en [R=301]
who make an redirection error.
And second case when i have to rewrite the same url but with different param:
RewriteRule ^the_url$ /other_url?lang=fr [R=301]
RewriteRule ^the_url?&lang=en$ /other_url?lang=en [R=301]
RewriteRule ^the_url?&lang=fr$ /other_url?lang=fr [R=301,L]
It works but only with the first one insruction.
(i dont put the flag L, cause i got a long list of rewrite; i think i can put the condition at the beginning and not rewrite them each-time)
:::::::::::::::::::::::
Exemple of real url that i am trying to redirect who cause trouble
www.mywebsite/-Avocats-a-la-Cour- to--> www.mywebsite/?lang=fr
www.mywebsite/-Avocats-a-la-Cour-?&lang=en to--> www.mywebsite/?lang=en
www.mywebsite/Marque-francaise?&lang=fr to--> www.mywebsite//Marque-francaise?lang=fr
www.mywebsite/French-Trademarks?&lang=en to--> www.mywebsite//French-Trademarks?lang=en
www.mywebsite/-Contact-?&lang=en to--> www.mywebsite/-Nous-contacter-?lang=en
www.mywebsite/-Contact-?&lang=fr to--> www.mywebsite/-Nous-contacter-?lang=fr
www.mywebsite/person-780?lang=en to--> www.mywebsite/personel?lang=en
You cannot match QUERY_STRING in RewriteRule. You can use:
RewriteCond %{QUERY_STRING} (^|&)lang=en(&|$)
RewriteRule ^-?Contact-?$ /-Nous-contacter- [R=302,NC,L]
QUERY_STRING is automatically carried over to target URI.
EDIT: Based on your edited question you can have these rules:
# www.mywebsite/-Avocats-a-la-Cour- to--> www.mywebsite/?lang=fr
RewriteCond %{THE_REQUEST} \s/+(-Avocats-a-la-Cour-)\s [NC]
RewriteRule ^ /?lang=fr [R=302,L,NE]
# www.mywebsite/-Avocats-a-la-Cour-?&lang=en to--> www.mywebsite/?lang=en
RewriteCond %{THE_REQUEST} \s/+(-Avocats-a-la-Cour-)\?&?(lang=en)\s [NC]
RewriteRule ^ /?%2 [R=302,L,NE]
# www.mywebsite/Marque-francaise?&lang=fr to--> www.mywebsite/Marque-francaise?lang=fr
# www.mywebsite/French-Trademarks?&lang=en to--> www.mywebsite/French-Trademarks?lang=en
RewriteCond %{THE_REQUEST} \s/+(Marque-francaise|French-Trademarks)\?&(lang=(?:en|fr))\s [NC]
RewriteRule ^ /%1?%2 [R=302,L,NE]
# www.mywebsite/-Contact-?&lang=en to--> www.mywebsite/-Nous-contacter-?lang=en
# www.mywebsite/-Contact-?&lang=fr to--> www.mywebsite/-Nous-contacter-?lang=fr
RewriteCond %{THE_REQUEST} \s/+(-Contact-)\?&?(lang=(?:en|fr))\s [NC]
RewriteRule ^ /-Nous-contacter-?%2 [R=302,L,NE]
# www.mywebsite/person-780?lang=en to--> www.mywebsite/personel?lang=en
RewriteCond %{THE_REQUEST} \s/+(person-780)\?&?(lang=(?:en|fr))\s [NC]
RewriteRule ^ /personel?%2 [R=302,L,NE]
This doesnt look right
RewriteRule ^-Contact-?&lang=en$ /-Nous-contacter-?lang=en [R=301,L]
I would think the & should only be used when you have more than one parameter being passed
e.g.
?lang=en&userId=13
I would try:
RewriteRule ^-Contact-?lang=en$ /-Nous-contacter-?lang=en [R=301,L]

Clean URL's with htaccess

I have downloaded the phpDolphon Social Networking Script. It is great, however, it does not have clean URLs. Does anybody know how to do it?
Here is an example of the index page:
mydomain.com/index.php?a=feed
and I want to change it to:
mydomain.com/
Here is an example of the profile page:
mydomain.com/index.php?a=profile&u=daniel
and I want to change it to:
mydomain.com/user/daniel
I have tried this but it doesn't seem to work. Any ideas?
RewriteEngine On
RewriteRule ^([A-Za-z0-9-+_%*?]+)/([A-Za-z0-9-+_%*?]+)/?$ index.php?a=$1&u=$2 [L]
How about:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+index\.php\?a=feed
RewriteRule ^ /? [L,R=301]
RewriteCond %{THE_REQUEST} \ /+index\.php\?a=profile&u=([^&\ ]+)
RewriteRule ^ /user/%1? [L,R=301]
RewriteRule ^user/([^/]+)$ /index.php?a=profile&u=$1 [L,QSA]
RewriteRule ^$ /index.php?a=feed [L,QSA]
You'll need very specific rules:
# no 'feeds' in alias
RewriteRule ^$ index.php?a=feed (maybe it needs a /)
# 'user' in one, 'profile' in other
RewriteRule ^user/(\w\d+)$ index.php?a=profile&u=$1
etc

Resources