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 ,.
Related
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 tried and tried with examples posted here, but i didn't manage to make my htaccess run properly.
Here's the situation:
i have links looking like this
domain.com/sport/football/index.php?lang_id=1&page_id=500 (home page)
domain.com/sport/football/index.php?lang_id=1&page_id=505 (players)
domain.com/sport/football/index.php?lang_id=1&page_id=510 (coaches) ...
i would like to rename them to
domain.com/sport/football/
domain.com/sport/football/players/
domain.com/sport/football/coaches/
etc...
and for all non-designated page_id's to redirect to home page.
All help is very much appreciated.
In the htaccess file in your document root, add:
RewriteEngine On
RewriteRule ^sport/football/$ /sport/football/index.php?lang_id=1&page_id=500 [L,QSA]
RewriteRule ^sport/football/players/?$ /sport/football/index.php?lang_id=1&page_id=505 [L,QSA]
RewriteRule ^sport/football/coaches/?$ /sport/football/index.php?lang_id=1&page_id=510 [L,QSA]
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /sport/football/index\.php
RewriteCond %{QUERY_STRING} ^(.*)page_id=500($|&)
RewriteRule ^ /sport/football/? [L,R=301]
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /sport/football/index\.php
RewriteCond %{QUERY_STRING} ^(.*)page_id=505($|&)
RewriteRule ^ /sport/football/players/? [L,R=301]
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /sport/football/index\.php
RewriteCond %{QUERY_STRING} ^(.*)page_id=510($|&)
RewriteRule ^ /sport/football/coaches/? [L,R=301]
You can use RewriteMap Directive for that. You must define the mapping from names to ids
players 505
coaches 510
and tell Apache about the map
RewriteMap football txt:/path/to/footballmap.txt
The RewriteMap must be in either the main configuration file or inside a VirtualHost directive.
Now you can use this map
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^sport/football/(.*)/?$ /sport/football/index.php?lang_id=1&page_id=${footballmap:$1|500} [L]
If there's no key found, the default 500 (homepage) will be used. If you have lots of mappings, you can also use a hashfile instead.
Update:
When you don't have access to the server or virtual host configuration file, you can only have a fixed RewriteRule "map"
RewriteRule ^sport/football/players/?$ /sport/football/index.php?lang_id=1&page_id=505 [L]
RewriteRule ^sport/football/coaches/?$ /sport/football/index.php?lang_id=1&page_id=510 [L]
# maybe other similar rules ...
# this is a catch everything else and must come last
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^sport/football/ /sport/football/index.php?lang_id=1&page_id=500 [L]
I have an .htaccess file that rewrites urls for SEO purposes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /display.php?page=$1 [L,NC,QSA]
RewriteRule ^search/([^/\.]+)/?$ search.php?what=$1&where=$2 [L,NC,QSA]
Now the first rewrite rule works fine. (www.domain.com/user goes to display.php?page=user)
but the second one should work like (www.domain.com/search/something/else must go to search.php?what=something&where=else
What am I doing wrong here?
Your second rule is incorrect for what you are looking for. You are requesting the result of two captures, but are only making one capture.
Try this instead:
RewriteRule ^search/([^/\.]+)/([^/\.]+)/?$ search.php?what=$1&where=$2 [L,NC,QSA]
Edit: You'll also need to switch your rules around. Your first rule captures everything, and would therefore discard the second.
So, swap them around, and use the L flag, as suggested already.
You are using the [L] flag, which causes mod_rewrite to stop processing the rule set.
Try removing the L flag from the first Rewrite Rule, like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /display.php?page=$1 [NC,QSA]
RewriteRule ^search/([^/\.]+)/?$ search.php?what=$1&where=$2 [L,NC,QSA]
I am trying to redirect wordpress default login (wp-logiin.php) url to signin but somehow it is not working. I have never done this .htaccess rewrite rule before so no idea how it works.
# BEGIN WordPress
RewriteRule ^signin$ http://localhost/newsite/wp-login.php [NC,L,R]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /newsite/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /newsite/index.php [L]
</IfModule>
# END WordPress
Big Big thanks
Try to read manual :)
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
First of all, if you want to use "rewrite", condition must be defined first, followed by rule which is used if is condition met. For example:
RewriteCond %{HTTP_USER_AGENT} ^facebookexternalhit [OR,NC]
RewriteCond %{HTTP_USER_AGENT} ^Zeus [NC]
RewriteRule ^.* /errors/404.html [F]
RewriteRule ^signin$ http://localhost/newsite/wp-login.php [NC,L,R]<br>
Use of RewriteRule without defined RuleCondition, thus IMHO this rule will not be used, never.
RewriteCond %{REQUEST_FILENAME} !-f<br>
RewriteCond %{REQUEST_FILENAME} !-d<br>
RewriteRule . /newsite/index.php [L]<br>
As say manual "Pattern is a perl compatible regular expression.", thus single "." as pattern mean one single character. So, if request filename will be "test.txt", it will replace every letter with "/newsite/index.php" (8 chars = 8x repeat).
Correct version is: RewriteRule ^.*$ /newsite/index.php [L]
You're redirecting logins to localhost, so unless you're using a browser from the same machine that your wordpress site is running on, you're probably going to get a connection error. Try removing the http:// bit from your rule:
RewriteRule ^signin$ /newsite/wp-login.php [NC,L,R]
I have URLs like:
1) http://www.example.com/?page=2
2) http://www.example.com/my-photos-folder?page=3
Here page number will increase sequentially (page=1, page=2, page=3 .....).
"my-photos-folder" Can be anything like "my-images-folder" or "Nice-photos" etc..
What i would like to get:
1) http://www.example.com/page/2
2) http://www.example.com/my-photos-folder/page/3
My .htaccess has the following rules:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^page\/(.*)$ index.php?page=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?kohana_uri=$1 [QSA,PT,L]
RewriteRule ^$ index.php?kohana_uri=$1 [QSA,PT,L]
RewriteRule ^index.php/(.*) $1 [QSA,R,L]
</IfModule>
Can any expert suggest me what rules i need to add so that my desired outcome will be achived. Thank you in advance.
Add theese lines after the RewriteEngine On line:
RewriteRule ^page\/(.*)$ index.php?page=$1 [L]
RewriteRule ^(.*)\/page\/(.*)$ $1?page=$2 [L]
#develroot
RewriteRule ^page\/(.*)$ index.php?page=$1 [L] //This is working fine for homepage
RewriteRule ^(.*)\/page\/(.*)$ $1?page=$2 [L] //this rule still not working on directory level.
The second one still have issue. Please consider the rules which are already there.[I have updated the question - added your first rule which work fine.]