What do I need to do to the following rewrite rule to make it so it works whether or not their is a slash at the end of the URL?
ie.
http://mydomain.com/content/featured
or
http://mydomain.com/content/featured/
RewriteRule ^content/featured/ /content/today.html
Use the $ to mark the end of the string and the ? to mark the preceding expression to be repeated zero or one times:
RewriteRule ^content/featured/?$ content/today.html
But I recommend you to stick to one notation and correct misspelled:
# remove trailing slashes
RewriteRule (.*)/$ $1 [L,R=301]
# add trailing slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ $0/ [L,R=301]
simple way to do this :
RewriteEngine On
RewriteBase /
RewriteRule ^content/featured(\/||)$ /content/today.html [L,R=301,NC]
Related
I am forcing trailing slashes on my website but it's caused an issue with our Wiki, as it treats them as a blank page.
Want I want to do is force trailing slash except if in /wiki/. How would I do this?
Here is my current rule for it:
# force trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]
You can add a RewriteCond (condition) directive to create an exception for any URL that starts /wiki/.
For example:
# force trailing slash
RewriteCond %{REQUEST_URI} !^/wiki/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]
The ! prefix on the regex ^/wiki/ negates the expression, so it is successful when it does not match.
You will need to clear your browser cache.
Recently I redesigned my site let's say http://www.sitename.com/ .
Before the redesign, the homepage url was something like this: http://www.sitename.com/default.asp?id=1&lg=1
Old pages had also weird query strings and they are not relevant any more, so, I want to redirect everything that begins with default.asp to the homepage.
RewriteRule default\.asp.* \
alternatively
RewriteCond %{QUERY_STRING} ^default.asp?id=1&lg=1$ [NC]
RewriteRule http://www.sitename.com/ http://www.sitename.com/? [R=301,L]
This is the closest I have got so far, but I am pretty sure its wrong.
Can you help?
Update: This is my whole .htaccess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule default\.asp.* /?
</IfModule>
<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
I have tried putting my rule at the top and at the bottom, (no luck). Should I embed it somehow on the other rule?
You can use these rules:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# redirect /default.asp to landing page
RewriteRule default\.asp$ /? [L,NC,R=301]
RewriteRule ^index\.php$ - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
# END WordPress
Based on this site, if you set up the rule like this
RewriteRule default\.asp.* /?
it should work.
Here is the reference for how you can replace query strings in the rewrite:
http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule
Modifying the Query String
By default, the query string is passed through unchanged. You can,
however, create URLs in the substitution string containing a query
string part. Simply use a question mark inside the substitution string
to indicate that the following text should be re-injected into the
query string. When you want to erase an existing query string, end the
substitution string with just a question mark. To combine new and old
query strings, use the [QSA] flag.
UPDATE
Based on your comment, try this:
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule default\.asp.*$ /? [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
Note the [L] at the end of RewriteRule default\.asp.*$ /? [L] which terminates the rewriting process when that match is found. See L flag
If you don't include the L flag, then the process will continue with the rest of the rules in your .htaccess until it reaches the end with no matches or until it matches one with the L flag. Think of it like a switch statement, which needs a break in each case or else it continues to the next case, if it helps you.
default.asp is a filename you need to match against it using %{REQUEST_URI} variable. it's not part of querystring. To redirect requested URIs that contain default.asp ,you can use the following :
RewriteCond %{REQUEST_URI} /default\.asp [NC]
RewriteRule ^ http://sitename.com/? [R,L]
I want to remove %20 on my link to - (dash),
My .htaccess is like that right now,
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ icerik.php?name=$1 [QSA,L]
For example,
site.com/vision-and-mision,
site.com/do-re-mi-fa-so-la-si.
Actually I searched something but the informations were very specific and I'm confused
Thank you.
You can use the following in your /.htaccess file:
RewriteEngine On
# Replace whitespace with hyphens, set the environment variable,
# and restart the rewriting process. This essentially loops
# until all whitespace has been converted.
RewriteRule ^([^\s]*)\s(.*)$ $1-$2 [E=whitespace:yes,N]
# Then, once that is done, check if the whitespace variable has
# been set and, if so, redirect to the new URI. This process ensures
# that the URI is rewritten in a loop *internally* so as to avoid
# multiple browser redirects.
RewriteCond %{ENV:whitespace} yes
RewriteRule (.*) /$1 [R=302,L]
Then add your rules afterwards:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /icerik.php?name=$1 [QSA,L]
If this is working for you, and you would like to make the redirects cached by browsers and search engines, change 302 to 301.
my urls are index.php?lang=1, index.php?lang=2, etc for each home language
and i want to rewrite it to index-en.html, index-de.html, etc
I missed something, it doesn't work.
the second other rule is messing up, don't know.
here is my code :
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^index.php?lang=1$ index-en.php [QSA, L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^/\.]+(\.html)?$ index.php?url=$0 [QSA,L]
</IfModule>
Any idea ?
Thanks
You can't match against the query string in a RewriteRule, you need to match against the %{QUERY_STRING} variable in a condition. So instead of:
RewriteRule ^index.php?lang=1$ index-en.php [QSA, L]
You want:
RewriteCond %{QUERY_STRING} ^lang=1$
RewriteRule ^index\.php$ index-en.php? [L]
You also don't want that space inside the brackets. It'll confuse mod_rewrite and make it think the rewrite flags end with ,.
I have following code in .htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)(/([^/]+))?(/(edit)+)(/([^/]+))?/?$ edit.php?secret=Y7qD7&category=$1&slug=$3&edit=$5&part=$7 [L]
RewriteRule ^([^/]+)(/([^/]+))?/?$ content.php?category=$1&slug=$3 [L]
RewriteRule ^(/)?$ content.php [L]
What I expect to achieve is
http://example.com/test/test1/edit/part to edit.php?category=test&slug=test1&edit=edit&part=part
http://example.com/test/edit/part to edit.php?category=test&slug=&edit=edit&part=part
(above rewrite is working as expected)
`http://example.com/test/test/` to `content.php?category=test&slug=test`
`http://example.com/test/` to `content.php?category=test&slug=`
(Please note that there is no "/edit/" & "/part/" in above 2 urls)
for above two rewrites, first one is working fine but the second one is not working as expected. The last one get rewrite to content.php?category=content.php&slug= which is not correct.
Also trailing slash should not make a difference for the rewrite.
Could somebody please show me what I'm doing wrong here?
I didn't check why the rule in your question doesn't work as expected, but you may try this instead:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([^/]+)/?([^/]*)?/?([^/]*)?/?([^/]*)?/?
RewriteRule .* edit.php?key1=%1&key2=%2&key3=%3&key4=%4 [L]
Maps silently:
http://example.com/val1/ up to
http://example.com/val1/val2/val3/val4/ with or without trailing slashes
To:
http://example.com/edit.php?key1=val1&key2=val2&key3=val3&key4=val4
The maximum quantity of valN values passed in the incoming URL, is 4. The minimum is 1. That range can be adjusted modifying the rule, though.
When any valN is not present in the incoming URL, the value in the corresponding key-value pair in the query added to the substitution URL, will be empty.
However, the key will always be present in the query as all keys are fixed strings not passed by the incoming URL.
This rule-set is tested and working and it should be tested without any other rule that might get in conflict with it. I didn't check the other rules in the question and can't say if they work or if they could affect this one. That was not part of the question.
UPDATE
Redirecting to edit.php:
Mapping to edit.php is required only when there are 3 or 4 folders in the URL-path.
The modified rule-set is:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !edit\.php [NC]
RewriteCond %{REQUEST_URI} ^/([^/]+)/([^/]+)/([^/]+)/?([^/]*)?/?$ [NC]
RewriteRule .* edit.php?key1=%1&key2=%2&key3=%3&key4=%4 [L,QSA]
Maps silently:
http://example.com/val1/val2/val3/ up to
http://example.com/val1/val2/val3/val4/ with or without trailing slashes
To:
http://example.com/edit.php?key1=val1&key2=val2&key3=val3&key4=val4
The maximum quantity of valN values passed in the incoming URL, is 4. The minimum is 3.
Redirecting to content.php:
Mapping to content.php is very similar to the previous one, except is done only when the number of folders is 1 or 2.
So the rule-set is basically the same with less regex groups:
RewriteCond %{REQUEST_URI} !content\.php [NC]
RewriteCond %{REQUEST_URI} ^/([^/]+)/?([^/]*)?/?$ [NC]
RewriteRule .* content.php?key1=%1&key2=%2 [L,QSA]
Maps silently:
http://example.com/val1/ up to
http://example.com/val1/val2/ with or without trailing slashes
To:
http://example.com/content.php?key1=val1&key2=val2
The maximum quantity of valN values passed in the incoming URL, is 2. The minimum is 1.
The complete rule-set is like this:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !edit\.php [NC]
RewriteCond %{REQUEST_URI} ^/([^/]+)/([^/]+)/([^/]+)/?([^/]*)?/?$ [NC]
RewriteRule .* edit.php?key1=%1&key2=%2&key3=%3&key4=%4 [L,QSA]
RewriteCond %{REQUEST_URI} !content\.php [NC]
RewriteCond %{REQUEST_URI} ^/([^/]+)/?([^/]*)?/?$ [NC]
RewriteRule .* content.php?key1=%1&key2=%2 [L,QSA]
Hope I understood what you want.