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.
Related
I want to create pretty url. But, I got some problem with .htaccess. For example I have url domain/some.php?f=query-string.
I want to change domain/query-string (expected url). Is that possible to change / redirect via .htaccess. Or maybe from php file itsself.
this is a bit of htaccess snippet i made, but i get it blank/error page
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteRule ^/([^/.]+)$ some.php?f=$1 [NC,L]
</IfModule>
Thanks for your attention.
RewriteRule ^/([^/.]+)$ some.php?f=$1 [NC,L]
In .htaccess, the URL-path matched by the RewriteRule pattern does not start with a slash, so the above will never match and it will do nothing. This should be written like the following instead:
RewriteRule ^([^/.]+)$ some.php?f=$1 [L]
The NC flag is not required here, since the regex is already "case-insensitive".
I am really struggling to get .htaccess correct. I basically want to convert
product.php?sku=M1234 for example to display as M1234.php and to be able to type ../M1234.php to take to perform that operation. I also want all pages on the website to be HTTPS.
I have tried this:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,N]
RewriteRule ^([^/]*)\.html$ /products/product.php?product_id=$1 [L]
With no success. Any advice would be most appreciated.
Many thanks
Use:
RewriteRule ^([A-Za-z0-9-]+)/?$ /products/product.php?sku=$1 [NC,L] # Handle product requests
This will use website.com/test to request website.com/products/product.php?sku=test
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]
I went through a bunch of websites and tutorials yet can't find a solution.
Following snippet works and http://example.com/page/pot return a pot.php content
RewriteEngine on
RewriteRule ^page/([^/]*)$ $1.php?page=$1 [L]
I can't get it to work the other way around
RewriteEngine on
RewriteRule ^([^/]*)/page$ $1.php?page=$1 [L]
Your current approach will cause infinite looping since Apache re-injects rewritten URI back for further rule processing.
You need to use THE_REQUEST variable for that like this:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+page/[^.]+\.php\?page=([^\s&]+) [NC]
RewriteRule ^ page/%1? [R=302,L]
# internal forward from pretty URL to actual one
RewriteRule ^page/([^/]*)/?$ $1.php?page=$1 [L,QSA,NC]
Try to use:
RewriteEngine on
RewriteRule ^([^/]+)/page$ $1.php?page=$1 [L]
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.