Question

I'm pretty new to .htaccess and have a question..

How would I rewrite

http://example.com/forums/view.php?v=Topic%20Name&a=64 to http://example.com/forums/view/Topic-Name/64?

as stated above, I'm new to this..

Also, how would I replace - with   while keeping normal dashes?

Example: Hi There-Bye! would then convert into Hi-There-Bye!

Which would then convert back to Hi There-Bye! with php and .htaccess?

Was it helpful?

Solution

The first question is pretty basic. You need an external redirect from the 'ugly' (but working) url to the seo url and an internal rewrite from the seo url to the 'ugly' url. They can all be written in the following format:

#External redirect
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /forums/view\.php\?v=([^&]+)&a=(.+)\ HTTP
RewriteRule ^ /forums/view/%1/%2 [R,L]
#Change above [R,L] to [R=301,L] after testing all rules work as expected

#Internal rewrite
RewriteRule ^forums/view/([^/]+)/([^/]+)/?$ /forums/view.php?v=$1&a=$2 [L]

The second thing is not possible. There is no way to distinguish between the dash between Hi and There and the dash between There and Bye. I would say: Don't bother. It's a seo-url. If you want to have fancy seo-url's, you'll have to alter your database to contain those fancy seo-urls.

OTHER TIPS

Rewrite using below method.

RewriteRule ^example/(.*)\.html$ example/test.php?param=$1
RewriteRule ^forums/view/([^/]*)/([^/]*)$ /forums/view.php?v=$1&a=$2 [L]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top