Tag Info

Hot answers tagged

12

Yes, if you have your URLs redirected properly. Yes. You should be using a 301 redirect instead of internally rewriting the URLs. No, this is completely unnecessary. If your old URLs are 301 redirected to the new URLs, then Google will know that those resources have been permanently renamed/moved. That's the whole point of having a 301 redirect code (as ...


10

Try this RewriteEngine On RewriteBase / RewriteRule ^edit/id/([0-9]+)/?$ edit.php?id=$1 [NC,QSA,L]


9

You are looking for the Query String Append flag - e.g. RewriteCond %{SCRIPT_FILENAME} !-d RewriteCond %{SCRIPT_FILENAME} !-f RewriteRule ^search/(.*)$ search.php?q=$1 [QSA] See the mod_rewrite documentation for a full description of RewriteRule flags.


8

For your first two rewrites (non-www → www, http → https), the following rule should work: RewriteEngine On RewriteCond %{HTTPS} off [OR] RewriteCond %{HTTP_HOST} !=www.example.com [NC] RewriteRule ^(.*) https://www.example.com/$1 [NS,L,R=permanent] Just replace www.example.com with the actual canonical hostname of your site. As for your third ...


6

Assuming your .htaccess is on the right place with the right permissions and your Apache server is configured to use it and mod_rewrite is enabled: RewriteEngine on RewriteCond %{HTTP_HOST} ^www.example.com$ RewriteRule ^/?(.*)$ http://example.com/$1 [R=301,L] Note the HTTP_HOST vs HTTP_POST in your example and that I use a 301 redirect. This is better ...


6

To implement such redirect using mod_rewrite and .htaccess you need to use RewriteMap directive which cannot be placed in .htaccess -- only in server config / VirtualHost context. If you have such access: 1. Place this line inside <VirtualHost> block for your site: RewriteMap lc int:tolower 2. Place this in your .htaccess: RewriteCond ...


6

It means "this matches the start of the string". So in your example the rule does the rewrite if there's the start of the string (^) any character (.) any number of characters (*) the end of the string ($) In other words, this whole expression matches any string.


6

The way that you will do the 301 redirect does not play any role for the Google. Actually it does not see your configuration file. What it is important is to send the 301 headers with the new location on each old url and not just the home page. For example if you have 10 pages on the old website, make sure that these all pages are redirected to the new 10 ...


5

What I do: use vhosts for each site, including a vhost for the variant I want to suppress. This keeps all the configuration for a named site in one place. The duplication of content is minimal thanks to mod_macro. You want to issue a redirect, not rewrite internally, since the idea is to get the client to retry using the correct protocol. Make sure to only ...


5

So I wrote a set of Rewrite rules that did what you wanted, but it completely broke my website. I realized that what you want is probably not what you need. Adding trailing slashes to the end of all URLs really messes with the semantics of the URL in that you're no longer accessing the file /foo but the content listing of the directory /foo/. For example: ...


5

The httpd.conf for campaign.com with it pulling data from site.com/macguffin/ <VirtualHost *:80> ServerName campaign.com <Proxy *> Order deny,allow Allow from all </Proxy> ProxyRequests Off ProxyPreserveHost Off ProxyPass / http://site.com/macguffin/ ProxyPassReverse / http://site.com/macguffin/ <Location /> Order ...


5

You need to add the following line into your .htaccess file(s): Options +MultiViews The effect of MultiViews is as follows: if the server receives a request for /some/dir/foo, if /some/dir has MultiViews enabled, and /some/dir/foo does not exist, then the server reads the directory looking for files named foo.*, and effectively fakes up a ...


5

Place this in your .htaccess file: RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /blog?display=$1 [L] This will redirect each URL which do not corresponds to an existing file (!-f) or existing directory (!-d) to the corresponding blog?... URL. [L] makes the rewriting stop here (in case you have ...


5

The common mistake that a lot of people do is trying to match whole URL including query string. The reality is: when matching URL, the pattern get applied to path part of it and query string has to be matched separately. In other words -- RewriteRule cannot be used to match query string directly -- only with help of RewriteCond. Considering the ...


5

Thanks for your time to look at the question, but we appeared to have figured it out: Options -Multiviews -Indexes +FollowSymLinks RewriteEngine On RewriteBase / DirectorySlash Off # remove trailing slash RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L] # rewrite /dir/file?query to /dir/file.php?query RewriteRule ^([\w\/-]+)(\?.*)?$ $1.php$2 ...


5

I will go onto something like that. First you define a new condition (RewriteCond) then you apply this condition using a rule (RewriteRule). Thanks to the flag L at the end of the RewriteRule, this stop the rewriting process immediately and don't apply any more rules. So when the request is a .css file, the first rule will be apply and not the second one. ...


5

Just redirect by 301 HTTP status all your old URLs to the new ones. If you only change file extension, you can do it easily with an .htaccess file (if you use Apache as a web server). Put these lines in your .htaccess file: RedirectMatch 301 (.*)\.html$ http://www.example.com$1.php Of course, change www.example.com by your domain name. By using 301 ...


4

ServerFault's Everything You Ever Wanted to Know about Mod_Rewrite Rules but Were Afraid to Ask topic is a great start - but, more to the point, you're looking for something like this: RewriteEngine on RewriteCond %{REQUEST_FILENAME} ^/([0-9]*)\.png$ RewriteRule ([0-9]*)\.png$ /create_image.php?count=$1 [L] Line 1: Enable the mod_rewrite engine (the ...


4

For setting custom 404s, you should be able to do that much easier (even in .htaccess), like so: ErrorDocument 404 http://example.com/?page_id=254 See also: ErrorDocument Less messy and significantly less CPU use per request than mod_rewrite - anything that 404s just goes straight there, no questions asked. I'd definitely consider making any errordoc a ...


4

It's a myth that "/pagename" or "/pagename.htm" are better for search engines than "/pagename.php". At least at Google, there is definitely no basis for this (and I assume the others as well). Similarly, even "index.php?page=pagename" does not need to be rewritten as "/pagename" -- search engines can understand those URLs without any problems, and Google has ...


4

My solution to this hasn't been to mess with the rewrite rules, but to add separate local domains/hosts with fake TLDs to my PC. You can do this by editing the file C:\Windows\System32\drivers\etc\hosts Be aware that on Windows 7 (and maybe Vista) that in order to successfully edit this file you need to run your text editor as Administrator. You can add ...


4

RewriteEngine On RewriteRule ^(.*)\.abc$ $1.php that should change all of your pages from .PHP to .ABC


4

This rule will redirect (301 Permanent Redirect) all incoming requests to xxx.yyy.site.com domain to http://xxx.site.com/ preserving the URL path (e.g. http://xxx.yyy.site.com/kitten?say=meow will be redirected to http://xxx.site.com/kitten?say=meow: RewriteEngine On RewriteCond %{HTTP_HOST} =xxx.yyy.site.com RewriteRule .* ...


4

If the user names (like kevinlee) are unique on the system, this is pretty straight forward. First, for simplicity, I would move the profile call to a profile.php page, and move it out of the /profile/ directory (I'm assuming it used to just be an index.php page in /profile/?). You don't have to do that, but it just makes the .htaccess work a little cleaner ...


4

Finally realised what the problem was when, after deleting the tag in both files, the error "Invalid command '\xef\xbb\xbf EF BB BF = BOM Moral of the story: Always check your encoding* and hex dump of your files when you get weird issues. *On Notepad++ you can use the Encoding -> Encode in UTF-8 without BOM option to remove it


4

You're almost there. Drop the very first RewriteCond (as it's not required and won't match ever) and add a trailing / to your last RewriteRule since you're redirecting with them. RewriteEngine On RewriteCond %{HTTP:Accept-Language} ^en [NC] RewriteRule ^$ http://mysite.com/en/ [L,R=301] RewriteCond %{HTTP:Accept-Language} ^de [NC] RewriteRule ^$ ...


4

You can use one of these solutions: Keep the Current .html extension unchanged for just frontend purpose and use Apache .htaccess file to proxify your requests so that. Add this to your .htaccess file to work: RewriteEngine on RewriteRule ^(.*).html $1.php OR Why not just have the HTML pages parsed as PHP? That way you can get the best of ...


3

Nevermind, figured it out. I found it's easier to match if something is in a string rather than if it isn't in a string, so this is what I came up with: ^.*\..*$ -> /$0 [stop matching if found] ^.*$ -> /index.php?u=$0 Not correct syntax of course, but you can easily adapt it to any number of rerouting systems.


3

The 3 step process you outlined above seems redundant and useless as is stated above. If in step 3 index.php takes them to the "real" page then why even bother with mod_rewrite at all? Doing that will negate the advantages mod_rewrite is offering. Namely, a search engine friendly URL and easier site maintenance. If you stop at step two you benefit from ...



Only top voted, non community-wiki answers of a minimum length are eligible