How to remove id and title from this url? [duplicate]
Asked Answered
E

4

6

I need to remove ?id= and &title= from this url using .htaccess file.

URL now - http://www.XXXX.com/video.php?id=XX&title=XXX-XXX-XXX

What I need - http://www.XXXX.com/video.php/XX/XXX-XXX-XXX

I already have removed .php from all links.

Ethics answered 21/4, 2015 at 7:0 Comment(3)
And what did you try ?Byler
use post method, Think you are trying this with getGentoo
RewriteRule ^video/([0-9]+)/([0-9a-zA-Z_-]+)$ video.php?id=$1&title=$2 [NC,L] this is not working..Ethics
I
6

Following htaccess code will do your work

    //First Parameer
    RewriteEngine On
    RewriteRule ^([a-zA-Z0-9_-]+)$ video.php?id=$1
    RewriteRule ^([a-zA-Z0-9_-]+)/$ video.php?id=$1

    //Second Parameter
    RewriteEngine On
    RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ video.php?id=$1&title=$2
    RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ video.php?id=$1&title=$2
Ileostomy answered 21/4, 2015 at 7:8 Comment(2)
Yep, that's far better than my version was. Nice.Elegancy
I don't see why this should be working. The OP wants to remove the querystring.Workman
E
1

add below code to your .htaccess file.

RewriteEngine on
RewriteBase / 
RewriteCond %{QUERY_STRING} ^id=(.*)\&title=(.*)
RewriteRule (.*) /$1/%1/%2?  [NC,L,R=301]

output is

http://www.XXXX.com/video.php/XX/XXX-XXX-XXX

this code is working for me i hope it is working for you.

Eclair answered 21/4, 2015 at 7:12 Comment(2)
your rule will match for any url with an id and title parameter, not only /video.phpWorkman
@MarioA Hey, i edit my code now it working with any file not only video.php ThanksEclair
W
1

In order to access the querystring you need to use a RewriteCond statement like this:

RewriteEngine on
RewriteCond %{QUERY_STRING} id=([^&]+)&title=([^&]+)
RewriteRule ^video.php$ video.php/%1/%2 [L,R=301]

The above example works when id and title parameters are in the exact order of your example. If there can be other parameters in the querystring, or if the parameters can be in any order, you would need to adjust the rules.

Workman answered 21/4, 2015 at 7:13 Comment(2)
why should it? RewriteRule ^video.php$matches just /video.php nothing else.Workman
Hey men, it's my mistake sorry. Thanks for the point it out.Eclair
E
1

Thanks you so much for all this answers.Subodh Ghulaxe have posted a good answer.

But this is working code for me.

# To externally redirect /dir/foo.php?id=123&title=456 to /dir/123/456
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?id=([^&]+)&title=([^&\s]+) [NC]
RewriteRule ^ %1/%2/%3? [R,L]

# To externally redirect /dir/foo.php?id=123 to /dir/123
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?id=([^&\s]+)\s [NC]
RewriteRule ^ %1/%2? [R,L]

# To internally forward /dir/foo/12 to /dir/foo.php?id=12
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/([^/]+)(/[^/]+)?/?$ $1.php?id=$2 [L,QSA]

RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ video.php?id=$1&title=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ video.php?id=$1&title=$2

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\s [NC]
RewriteRule ^ %1 [R,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*?)/?$ $1.php [L]

Sumurai8 finished this code in here ( complete .htaccess code ). I hope this code will help to someone.For css, js, images make sure to use absolute paths.

Ethics answered 23/4, 2015 at 9:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.