htaccess redirection for double slashes - .htaccess

I am having a problem with my site. Sometimes after the user log in is redirected to a page like this:
mydomain.com//somepage
Please notice the double slash in the URL, which takes to a not valid page so i want to modify my htaccess in order to make all pages with double slash to automatically redirect to an URL like this:
mydomain.com/folder/somepage
Please notice the word "folder" between slashes this time. An URL like this would always take to a valid page.
I made some rewrite rules for my htaccess but they dont work as expected:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{THE_REQUEST} \ //([^\?\ ]*)
RewriteRule ^ /folder/%1 [L,R=301]
</IfModule>
Could you please give me a hint?
Thank you.

Try something like this:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ //([^\?\ ]*)
RewriteRule ^ /folder/%1 [L,R=301]
You need to match against the %{THE_REQUEST} variable because the URI gets normalized before it gets matched against the pattern of rewrite rules.

Related

How to redirect a page with a query string to the homepage?

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]

.htaccess and rewrite rule 404 not found

I'm new on .htaccess and rewrite rules.
So if my question is not relevant, please forgive me.
I have below htaccess code.
RewriteRule ^([^/]+)/([^/]+)/?$ article-list.php?link=$1&page=$2 [L,QSA]
If i visit url like www.example.com/category/0 it works.
But if i strip page url and last slash www.exapmle.com/category i see an ugly 404 page.
What is wrong with my htaccess directive?
Thanks in advance.
EDIT: Compeletely .htaccess
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]
RewriteRule ^haber/([^/]+)-([^/]+)/?$ article.php?link=$1&i=$2 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ article-list.php?link=$1&page=$2 [L,QSA]
This is a correct behaviour.
Actually, in a regular expression, a + means at least one.
When you use ([^/]+) it means at least one character which is not a slash.
Your rule ^([^/]+)/([^/]+)/?$ means at least one character which is not a slash / at least one character which is not a slash optional slash.
That's why it does not work with only the first part url.
If you also want to handle example.com/category you'll need another rule:
RewriteRule ^([^/]+)/?$ article-list.php?link=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ article-list.php?link=$1&page=$2 [L]

Cancel rewrite and redirect removing last directory with htaccess

I have a page with this settings:
Options All -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /demo/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !^(.*)show(/|.*)$
RewriteRule . /demo/index.php [L]
</IfModule>
For example, when you request the page http://example.com/demo/any_folder/ it loads http://example.com/demo/index.php without changing the URL, to retrieve data from a database for example. Well, this works.
But I want to add and exception.
I need a condition that if you visit http://example.com/demo/any_folder/show it loads the same URL removing the word "show" but avoiding the redirect that is actually defined. So, it will load http://example.com/demo/any_folder/ and not http://example.com/demo/index.php. Does anyone know how to do this?
There has to be something different about the URL to not be redirected to index.php. The following .htaccess code adds a query parameter ?show to tell the two URLs apart.
Since, the redirection is internal; the user never gets to see the parameter.
RewriteEngine On
RewriteBase /demo/
RewriteRule ^index\.php$ - [L]
RewriteRule ^(.*)/show/?$ $1?show [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{QUERY_STRING} !^show [NC]
RewriteRule . /demo/index.php [L]

301 Redirect - variable in the old url

I have several urls on a Joomla site which have been indexed and I need to 301 redirect them into some new pages. The old URL is formed like this:
http://www.mydomain.com/en/wfmenuconfig/family/family-disease/177-category-english?start=20
I want it to go to:
http://www.mydomain.com/en/family-members/family-disease
I tried using:
RewriteCond %{QUERY_STRING} ^start=(.*)$
RewriteRule ^/en/wfmenuconfig/family/family-disease/177-category-english$ http://www.www.mydoamin.com/en/family-members/family-disease%1 [R=301,L]
I've tried several answers on here but nothing seems to be working.
htaccess 301 redirect dynamic url
and
301 Redirecting URLs based on GET variables in .htaccess
Any ideas what I should try next? (I've tried a normal redirect 301)
You've almost got it. You need to remove the leading slash from your rule's pattern because it's removed from the URI when applying rules from an htaccess file:
RewriteCond %{QUERY_STRING} ^start=(.*)$
RewriteRule ^en/wfmenuconfig/family/family-disease/177-category-english$ /en/family-members/family-disease%1? [R=301,L]
You also don't need the http://www.www.mydoamin.com bit (2 sets of www). At the end of your target, you have family-disease%1, which means if start=20 then the end of your URL will look like: family-disease20. Is that right?
The new URL doesn't have the query string in it, so it is just stripping of the last URL path part. If you want it hardcoded
RewriteCond %{QUERY_STRING} ^start=
RewriteRule ^en/wfmenuconfig/family/family-disease/177-category-english$ /en/family-members/family-disease? [R,L]
or a little bit more flexible
RewriteCond %{QUERY_STRING} ^start=
RewriteRule ^en/wfmenuconfig/family/family-disease/.+$ /en/family-members/family-disease? [R,L]
or if you just want to keep two levels after en/wfmenuconfig
RewriteCond %{QUERY_STRING} ^start=
RewriteRule ^en/wfmenuconfig/(.+?/.+?)/ /en/$1? [R,L]
Never test with 301 enabled, see this answer Tips for debugging .htaccess rewrite rules for details.
If you just want to redirect http://www.mydomain.com/en/wfmenuconfig/family/family-disease/177-category-english?start=$var into http://www.mydomain.com/en/family-members/family-disease, then you must try these directives:
# once per .htaccess file
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{QUERY_STRING} start=([0-9]*)
RewriteRule ^en/wfmenuconfig/family/family-disease/177-category-english /en/family-members/family-disease [R=301,L]
But if that's not what you want, but to redirect http://www.mydomain.com/en/wfmenuconfig/family/family-disease/177-category-english?start=$var into http://www.mydomain.com/en/family-members/family-disease$var then you could check this one:
# once per .htaccess file
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{QUERY_STRING} start=([0-9]*)
RewriteRule ^en/wfmenuconfig/family/family-disease/177-category-english /en/family-members/family-disease%1 [R=301,L]
Now, give this one a little more try if it will work. If it's not, then find any suspicious why this code is not working:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /en/
RewriteCond %{QUERY_STRING} start=([0-9]*)
RewriteRule ^wfmenuconfig/family/family-disease/177-category-english /family-members/family-disease [R]
And go to http://www.mydomain.com/en/wfmenuconfig/family/family-disease/177-category-english?start=$AnyNumber if it's redirecting into http://www.mydomain.com/en/family-members/family-disease just make sure that your web server have mod_rewrite.
I just wanted to throw this out there, I was also having trouble getting the RewriteRule to work. I have a client that upgraded to a WordPress powered site from .asp pages. What I had to do to get this to work is insert the RewriteCond and RewriteRule in the htaccess file BEFORE the "# BEGIN WordPress" section. Now it works just as it should.
This is posted way late, but hopefully it helps someone else out there running into the same issue.
Doesn't Work:
# BEGIN WordPress
<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
RewriteCond %{QUERY_STRING} ^var=somestring$ [NC]
RewriteRule ^oldpage\.asp$ http://www.domain.com/newpage? [R=301,L]
Does Work:
RewriteCond %{QUERY_STRING} ^var=somestring$ [NC]
RewriteRule ^oldpage\.asp$ http://www.domain.com/newpage? [R=301,L]
# BEGIN WordPress
<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
Order of operations must be important =)

301 redirect from query string to simple URL

I've had a good look through the first ten pages of search results for "301 redirects" and can't find the answer so here goes...
I've moved from a crappy old CMS that didn't give my pages nice URLs to one that does and I want to set up a 301 redirect for my key pages.
Current URL: http://www.domain.com/?pid=22
New URL: http://www.domain.com/contact/
I'm using Wordpress and my current htaccess file looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Any help would be awesome!
Give this a try. All you need to do is check to see if you are on page X and then redirect to page Y. Consider RewriteCond statements to be 'if' statements. If you need more redirects just duplicate the last two lines and edit the paths.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.domain\.com\/?pid=22$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/contact [L,R=301]
You have to check the query string for the value in the "pid" variable and then redirect if the value in that variable matches a page you want to redirect. You can do this with the "RewriteCond" and "RewriteRule" directives like this:
RewriteEngine On
RewriteBase /
# Redirect pid=22 to http://www.domain.com/contact
RewriteCond %{QUERY_STRING} ^pid=22$
RewriteRule ^(.*)$ http://www.domain.com/contact [R=301,L]
You can repeat the "RewriteCond" and "RewriteRule" directives to create additional redirects.

Resources