Redirect AND rewrite url htaccess - php

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]

Related

.htaccess for redirecting dynamic URL not working

I want to rewrite my URL:
http://jainpopulationregister.com/page.php?action=about
to:
http://jainpopulationregister.com/page/action/about/
with URL rewriting
My current URL rewrite code is as follows:
Options +FollowSymLinks
RewriteEngine on
RewriteRule page/action/(.*)/ page.php?action=$1
RewriteRule page/action/(.*) page.php?action=$1
But when I place this in my root folder, nothing seems to happen. What am I doing wrong?
You want to redirect from
http://jainpopulationregister.com/page.php?action=about
to
http://jainpopulationregister.com/page/action/about/
but your redirect rule does exactly the opposite. Assuming that you really want to redirect from /page.php?action=about to /page/action/about/, use the following configuration in htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^action=(.*)$
RewriteRule ^(.*)$ /page/action/%1/? [R=302,L]
RewriteRule ^page/action/([a-zA-Z0-9]+)/$ pages.php?action=$1 [NC,L]
In "pages.php" file get the action $_GET['action'];

Issue with Rewrite Rule in .htaccess file

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]

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.

mod_rewrite and 301 redirect in the same htaccess

I have the following .htaccess file:
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule (.*)$ ./page.php?name=$1
I also have about 20 different static addressess I want to do a 301 redirect to.
It should look something like:
Redirect 301 http://www.example.com/category.php?id=3 http://www.example.com/books
Redirect 301 http://www.example.com/articles.php?id=124 http://www.example.com/birds
I tried every method and failed.
Can somebody help me?
I'm guessing your after a set of SEO friendly URL's for those in your redirect examples, if this is the case then you don't want a browser redirect, instead you want an Internal Redirect or Alias e.g.
RewriteRule ^/?books /category.php?id=3 [L]
RewriteRule ^/?birds /articles.php?id=124 [L]
If you have many rules and have access to the httpd.conf you may want to consider a rewrite map file and rule.
Per your comment BELOW, if you want to force users/crawlers to the new URL structure then you'll need an additional set of Rewrite rules in the file (not redirects) e.g.
RewriteCond %{THE_REQUEST} /category\.php [NC]
RewriteCond %{QUERY_STRING} ^id=3 [NC]
RewriteRule ^.* http://%{HTTP_HOST}/books? [R=301,L]
FYI: The Apache mod_rewrite documentation is worth a read if your unclear as to what the above rules do, or if you want something a little more abstract consider the following post.

redirecting all dynamic urls to index page

as i want to redirect all dynamic urls like http://www.mysite.com/play.php?id=4820 to http://www.mysite.com/
i have so many dynamic urls i dont want my user to see the page like page not found etc. so whenever a user try to access the dynamic url like the above he should be redirected to home page. please can anyone tell me how to achieve this using .htaccess
Regards,
phphuger.
Enable mod_rewrite and put this code in your .htaccess under DOCUMENT_ROOT:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+play\.php[\s\?] [NC]
RewriteRule ^ / [L,R]
I think this should do.
RewriteRule ^\?.*$ http://www.mysite.com
EDIT2
Ok, now I think understand your question.
You have url at play.php that used to handle all your urls and you want them to be redirected to the homepage.
Here are two possible solutions. This is the first:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/play.php* [NC]
RewriteRule . /? [L,R=301]
</IfModule>
The above will redirect any requests to play.php to your home page using a 301 redirect, which means the redirect is permanent. This is different than a 302 redirect which is temporary. So it is important to redirect using 301.
Also in this rewrite rule, we don't care if the url exists or not. So you don't need to have the actual play.php file or anything. It just matches based on the url.
On the line for RewriteRule, there is a question mark at the end, this is to erase the query string from the url on redirect. I'm assuming you don't want the query string to carry over.
The [NC] is for case insensitive matching, so /PlAy.php would be redirected also
An alternative is use the Redirect directive:
Redirect 301 /play.php http://www.mysite.com/
This will do a 301 permanent redirect to your homepage if the user tries go to play.php. The only downside of this is that the query string shows up. You can add a question mark at the end and it will erase the query string. Unfortunately the question mark stays.
If you happen to have multiple endpoints and not just play.php, you can do the following:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/play\.php* [NC, OR]
RewriteCond %{REQUEST_URI} ^/another_url\.php* [NC]
RewriteRule . /? [L,R=301]
</IfModule>
This RewriteRule will match play.php or another_url.php and do 301 redirect to your home page.
Alternatively:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/(play|another_url)\.php* [NC]
RewriteRule . /? [L,R=301]
</IfModule>
Here is the alternative using the Redirect directive
Redirect 301 /play.php http://www.mysite.com/
Redirect 301 /another_url.php http://www.mysite.com/
Alternative you could also use the RedirectMatch directive to use regex expressions (Note I haven't actually tested this one)
RedirectMatch 301 /(play|another_url)\.php http://www.mysite.com/
EDIT
Ignore this answer. I misread your question. The following redirects all requests to index.php unless the file or directory exists, which is not what you want now that I read your question.
Here is how Wordpres does it. I believe Zend and Joomla also have variations of this.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Categories