Issue with Rewrite Rule in .htaccess file - php

I was about to convert my webpage url into seo friendly. My url link is like follows
http://example.com/display.php?id=***
Now i want to convert this into seo friendly url, which could be
http://example.com/partners-id-***.html
Something like above. I used
RewriteEngine On
RewriteRule ^partners-id-([^-]*)\.html$ /display.php?id=$1 [L]
Rewrite rule in htaccess file. But still the url stays same as older one. Anyone can help me to fix this issue???

You need 1 more 301 rule before this rule for external redirection:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+display\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /partners-id-%1.html? [R=301,L]
RewriteRule ^partners-id-([^.]*)\.html$ /display.php?id=$1 [L,QSA]
As commented by Justin and other folks it is better to change your links to /partners-id-123.html. However for links already cached by search engines will be taken care by first rule here which will 301 redirect:
/display.php?id=1234 => /partners-id-1234.html

Please try like below:
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /display.php?id=$1 [L]

Related

Query string url to seo friendly url

i am trying to rewrite a URL for SEO purpose.
The old URL is:
http://www.example.com/recipe_local.php?hl_cusine=1
The new URL should be like bellow and automatic redirect to this url if user come above url
http://www.example.com/recipes/healthy-recipes
My Code in the .htaccess is:
RewriteEngine on
RewriteRule ^recipes/healthy-recipes/$ recipe_local.php?hl_cusine=$1 [NC,L]
RewriteRule ^recipes/healthy-recipes$ recipe_local.php?hl_cusine=$1 [NC,L]
Even after hours of research, i have no clue why this is not working :(
The $1 in your rewrite is pointing to a backreference you never capture.
Unless you have a RewriteCond you are not showing?
try:
RewriteEngine on
RewriteRule ^recipes/healthy-recipes/$ recipe_local.php?hl_cusine=1 [NC,L]
RewriteRule ^recipes/healthy-recipes$ recipe_local.php?hl_cusine=1 [NC,L]

How to make clean URLs display dirty URLs with .htaccess?

I have a slight problem.
I have a site that uses these formats for viewing certain PHP documents:
domainname.com/server.php?id=14
domainname.com/changeserver.php?id=14
domainname.com/customize.php?id=14
I would like to make these:
domainname.com/server/14
domainname.com/changeserver/14
domainname.com/customize/14
I also have various URLs such as /index.php and /login.php I would like to clean up.
If someone were to type the original URL, such as /server.php?id=14, I would like it to redirect to it's clean URL.
If anyone can provide me with a config or help me along the way to making one that can do this, it would be greatly appreciated.
To prevent an infinite loop while externally redirecting the ugly url to the clean url and internally rewriting it back to the ugly url, you can use the THE_REQUEST trick.
#External redirect
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /(server|changeserver|customize)\.php\?id=([^&]+)\ HTTP
RewriteRule ^ /%2/%3? [R,L]
#Change above to [R=301,L] once all rules work as expected
#Internal rewrite
RewriteRule ^(server|changeserver|customize)/(.*)$ /$1?id=$2 [L]
You need to rewrite your URLs in .htaccess file.
RewriteEngine On
RewriteRule ^server/(.*)$ server.php?id=$1 [R=301,L]
here is a good guide on URL rewriting
http://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
In this case you have to use a 301 Redirect of URLS. Here is an example:
Redirect
www.example.com/a1/articles.php?id=1&name=abc
to
www.example.com/article/1/abc
For this you have to modify the .htaccess file with the following:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=(.*)&name=(.*)$
RewriteRule ^articles\.php$ /article/%1/%2? [R=301]
RewriteRule ^article/([^-]+)/([^-]+)$ /articles.php?article_id=$1&article_name=$2 [L]
RewriteEngine On
RewriteRule ^server/([A-Za-z0-9-_,()'+]+)?$ server.php?id=$1
RewriteRule ^changeserver/([A-Za-z0-9-_,()'+]+)?$ changeserver.php?id=$1
RewriteRule ^customize/([A-Za-z0-9-_,()'+]+)?$ customize.php?id=$1
To adapt to your code. Here is the answer.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^server\.php$ /server/%1? [R=301]
RewriteRule ^server/([^-]+)$ /server.php?id=$1 [L]
Check the Above code. I think this will help you.

Redirect AND rewrite url htaccess

I want to rewrite a URL and I don't want existing links to go to the old page (address) any more.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^about-us$ /shop/static_page\.php\?static_page_id=3 [NC,L]
This is what my .htaccess file looks like at the moment, and it works fine - although when I add in a normal redirect rule such as:
Redirect 301 /shop/static_page.php?static_page_id=3 http://example.com/about-us
This doesn't work - it's as if this line doesn't exist. Any ideas please?
2 things:
Ordering of rule is important so have 301 before your internal rewrites
Don't mix mod_alias rules with mod_rewrite
Following code should work for you:
Options +FollowSymlinks
RewriteEngine on
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+shop/static_page\.php\?static_page_id=3[\s&] [NC]
RewriteRule ^ /about-us? [R=301,L]
# internal forward from pretty URL to actual one
RewriteRule ^about-us/?$ /shop/static_page\.php?static_page_id=3 [NC,L,QSA]

Url Rewriting like Subdomain in PHP

I want to rewrite url as like subdomain style.
For example if main url is mysite.com/test than i want to rewrite it like test.mysite.com
mysite.com/test => test.mysite.com
Can you please suggest me what rewrite rule should i need to add in .htaccess file.
Thanks in advance.
RewriteCond %{HTTP_HOST} ^mysite.com$ [NC]
RewriteRule ^(.+)$ http://$1.mysite.com [L,R=301]
Should do it, might need a little tweaking

.htaccess Remove PHP get data from URL

Well I'm making profile pages so right now it looks like this
http://example.com/random/?user=Robert
What I want to do is remove ?user= from the URL so the page appears as
http://example.com/random/Robert
Iv'e searched and I can't find anything working for me.
Thanks!
Based on http://statichtml.com/2010/mod-rewrite-baseon-on-query-string.html this should do the trick:
RewriteCond %{QUERY_STRING} ^user=(.*)$ [NC]
RewriteRule ^random$ random/$1 [NC,L,R=301]
The first step is to make all of your links in this form http://example.com/random/Robert, then in the htaccess file in your document root, add:
RewriteEngine On
RewriteRule ^random/(.+)$ /random/?user=$1 [L]
But to handle 301 redirects to your old URLs, you can include this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /random/\?user=([^\ ]+)
RewriteRule ^random/$ /random/%1 [R=301,L]

Categories