Hot answers tagged mod-rewrite
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 ...
8
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.
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
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.
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
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
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 ...
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. ...
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
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
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
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 ...
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
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 ...
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
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 ...
3
Would it not be easier to inject the hostname into the rewrite rule via a RewriteCond back-reference?
UseCanonicalName Off
# include the IP address in the logs so they may be split
LogFormat "%A %h %l %u %t \"%r\" %s %b" vcommon
CustomLog /var/log/apache2/vaccess.log vcommon
RewriteEngine On
# a ServerName derived from a Host: header may be any case at ...
3
The $ operator will let you extract backreferences from matches in rewrite rules and the % operator will let you extract references from conditions.
RewriteCond %{HTTP_HOST} !www.mysite.com$ [NC] # Presuming you don't want to do www
RewriteCond %{HTTP_HOST} ^(.*)\.mysite\.com [NC] # Catch subdomain
RewriteCond %{REQUEST_URI} !index\.php [NC] # Don't rewrite ...
3
You seems to have quite a few domains and subdomains pointing to the same folder (so they share the same .htaccess) which makes your original approach not suitable for such situation. Use these rules instead of yours:
RewriteCond %{HTTP_HOST} =mydomain1.co.uk [OR]
RewriteCond %{HTTP_HOST} =mydomain1.com
RewriteRule ^(.*)$ http://www.mydomain1.com/$1 ...
3
You are trying to match a path that starts with a letter or number, but it starts with a '/'. Your rules should be something like
RewriteRule ^/([A-Za-z0-9-])/...$ ...
Alos, do you have access to the logs? If so, enable the logs for mog_rewrite
RewriteLog /some/path
RewriteLogLevel 3
3
Generate a WordPress-specific .htaccess file in your /blog/ directory by logging into the WordPress admin area, clicking 'Permalinks' (under the 'Settings' header), scrolling to the bottom of the page, and clicking 'Save Changes'.
This will force WordPress to create its default .htaccess file in your /blog/ directory, which includes all mod_rewrite rules. ...
3
Creating a huge .htaccess can have a serious performance impact on your system as it is read linearly for all requests, at least until a rule with the L attribute (Last Rule) is matched.
The way I did something similar (about 3000 redirects) is to put a custom error page in the .htaccess. From memory the syntax is something like this:
ErrorDocument 404 ...
3
You CAN check for the existence of a resource identified by an HTTP URL using mod_rewrite
See example below:
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteCond %{REQUEST_URI} ![.]html$
RewriteCond %{REQUEST_URI} ^([^.]*)
RewriteCond %1.html -U
RewriteRule ^.*$ $0.html ...
Only top voted, non community-wiki answers of a minimum length are eligible