luckr
luckr

Reputation: 73

.htaccess RewriteRule Automatically URL

Well i got this code at my .htaccess

RewriteRule page/(.*) ?page=$1
RewriteRule search/(.*) search.php?search=$1
RewriteRule post/(.*) post.php?id_post=$1

And it works but i need to change manually the url.

And i have alots of pages to change it.

Theres any other way to put code at .htaccess and make the browser gets the proper url automatically?

Example:

http://example.com/post.php?id_post=1

http://example.com/post/1

Upvotes: 1

Views: 643

Answers (1)

richhallstoke
richhallstoke

Reputation: 1601

# Redirect old Page URLs to new URLs
# Old URL format: http://example.com/?page=pagename
# New URL format: http://example.com/page/pagename
RewriteCond %{QUERY_STRING} ^page=(.*)$
RewriteRule ^(.*)$ page/%1? [R=301,L]

# Redirect old Search URLs to new URLs
# Old URL format: http://example.com/search.php?search=keyword
# New URL format: http://example.com/search/keyword
RewriteCond %{REQUEST_URI} search.php
RewriteCond %{QUERY_STRING} ^search=(.*)$
RewriteRule ^(.*)$ search/%1? [R=301,L]

# Redirect old Post URLs to new URLs
# Old URL format: http://example.com/post.php?id_post=1
# New URL format: http://example.com/post/1
RewriteCond %{REQUEST_URI} post.php
RewriteCond %{QUERY_STRING} ^id_post=([0-9]*)$
RewriteRule ^(.*)$ post/%1? [R=301,L]

# ** Any requests with an old URL should have been processed before this comment,
# ** and any requests with a new URL should only be processed by these rules:

# Support new SEO-friendly URLs
RewriteRule page/(.*) index.php?page=$1
RewriteRule search/(.*) search.php?search=$1
RewriteRule post/(.*) post.php?id_post=$1

Upvotes: 2

Related Questions