I'm trying to redirect an url that gives me an error 404.
I need to redirect 'website.com/example/sub-example' to 'website.com/example2'
Note that in my .htaccess file 'example2' already exists because is and url for another redirection, but every time I run the url 'website.com/example/sub-example' it jumps to 'website.com/example2/sub-example' and gives me an 404. I don't want the sub-directory on the redirect URL. Wish I was clear enough. And if anyone help me because I really don't know what to do.
If can help I'm using WP and this one below is my .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
UPDATE: I resolved it. Basically “example2” was at the top of my redirected item so every time I tried to run ‘website.com/example/sub-example’ it was running ‘website.com/example2/sub-example’and eventually that page doesn’t exist. I simply moved every redirect with more than one subfolder (or subdirectory) at the top of the redirected pages and everything seems to work.
So after 8h of work I understood that the redirection pages works like works sex… “Dominate” who is at the top.
Related
I am trying to redirect the following...
http://example.com/blog/article-name to
http://example.com/blog/news/article-name
.. and this works fine..
RewriteCond %{REQUEST_URI} !^/blog/news
RewriteRule ^blog/(.*) /blog/news/$1 [QSA,L,R=301]
It stops there being an infinite redirect loop...
However, I do not want the URL..
http://example.com/blog/
to redirect anywhere as this is my landing page to list all blog entries... At the moment I get an infinite loop here.
http://example.com/blog/news/news/news/news/news/news/news/news/news/news/news/news/news/news/news/news/news/news/
I have tried adding in another condition to stop the root page being redirected...
RewriteCond %{REQUEST_URI} !^/blog/$
but this does not work...
FYI
Here is my complete .htaccess file
# BEGIN WordPress
# The directives (lines) between `BEGIN WordPress` and `END WordPress` are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<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 %{REQUEST_URI} !^/blog/news
RewriteRule ^blog/(.+) /blog/news/$1 [L,R=301]
Any ideas?
Thanks all for your help... I have resolved this issue, by changing new root path, so there is no conflict, and my redirects work fine now. I think the issue I was experiencing was actually to do with cached urls routes in Chrome which are still happening in Incog mode, so even though I was making changes i was still being routed to previous settings.
RewriteCond %{REQUEST_URI} !^/blog/news
RewriteRule ^blog/(.*) /blog/news/$1 [QSA,L,R=301]
These directives should not cause a redirect loop when requesting /blog/, however, they will redirect you to /blog/news/. To prevent this undesirable redirect you can simply change the subpattern .* (0 or more) to .+ (1 or more), so that it matches /blog/<something> and not /blog/<anything>.
In other words:
RewriteCond %{REQUEST_URI} !^/blog/news
RewriteRule ^blog/(.+) /blog/news/$1 [L,R=301]
The QSA flag is not required here since you are not adding a query string in the substitution string.
UPDATE: You've put the directives in the wrong place - they need to go before the # BEGIN WordPress section, at the very top of your .htaccess file. By placing them at the end they simply aren't going to get processed.
However, this still isn't the cause of the redirect loop.
You do, however, need to make sure you've cleared your browser cache. Any erroneous 301s (perhaps during testing) will have been persistently cached by the browser. First test with 302 (temporary) redirects to avoid cching issues.
I'm trying to direct all traffic to the homepage only to a php script called go.php that gets a variable from the URL.
If someone visits domain.com/username go.php gets the username, looks up their information, saves the information to a session and then redirects to index.php and displays a modified version of the homepage (same domain) that has the retrieved information. Everything works except the mod rewrite part.
I tried the following and am not sure what I am doing wrong:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^/$ go.php?id=$1 [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
My logic was that if a request is to index.php it should be allowed, to prevent looping.
If the request is to the homepage it will go to go.php?id=username and the that script will redirect to index.php and trigger the prior mod rewrite rule to prevent looping.
Otherwise, it will do the regular redirect to index.php if the directory or filename doesn't exist.
Any thoughts on how to fix this?
I think this is what you mean:
RewriteRule ^(.*)$ go.php?id=$1 [QSA]
Explanation:
I'm looking at the ^/$ in the regular expression in the go.php rewrite rule. I've tested that and it appears to be an impossible match in that situation. One might be wanting to capture requests for root. But the forward slash is not passed to this portion of the RewriteRule for root. so a call for root (only) is ^$. And there's also no capturing parenthesis to feed the $1 you have appended to go.php?id=$1.
If none of my RewriteRule [L] matches, I want to redirect to a nice url /you/shall/not/pass, but show contents of /index.html.
This is what I am doing now (this is the very last rule in the file):
RewriteCond %{REQUEST_FILENAME} !-f # is not file
RewriteCond %{REQUEST_FILENAME} !-d # is not directory
RewriteCond %{ENV:REDIRECT_STATUS} !=200 # wasn't already redirected
RewriteRule .* index.html
It works fine, but keeps whatever garbage was written in the URL. I want it to be changed.
Doing this didn't work
#RewriteCond same as above
RewriteRule .* /you/shall/not/pass [R]
RewriteRule ^/you/shall/not/pass index.html
I apparentely don't understand how [R] works, whether it continues forwarding the changed url to other RewriteRules or not and what page it redirects to when the end of file is reached.
You can use ErrorDocument 404 with a rewrite rule for this:
Options +FollowSymLinks
ErrorDocument 404 http://domain.com/you/shall/not/pass
Then create a symbolic link like this:
/public_html/you/shall/not/pass -> /public_html/index.html
Replace /public_html/ with your DocumentRoot path.
So, after countless hours I made it work, somehow, I guess...
I am too tired to investigate, but I will get back later and edit if it is wrong.
ErrorDocument 404 http://domain.com/you/shall/not/pass # redirects if document does not exist
RewriteEngine on
RewriteRule ^$ /you/shall/not/pass [L,R] # redirects if request URI is empty, skips rest
RewriteRule ^you/shall/not/pass$ /index.html [L] # puts different resource to matched url
It's kinda weird that the solution is that short.. How come I didn't try that already?
I am working on a php redirect script which 302 redirects visitors to other sites when they access a redirect url..
The script gets a variable (id) from the url and then redirects the visitor to the specific page.
The url structure is : example.com/redirect/index.php?id=test
At the moment all redirects work if I use "ugly" urls, but I want to strip all unnessecary information out of the url with .htaccess rewrites for better usability.
Which .htaccess rewrite rules do I need to make the above shown urls look like : example.com/redirect/test
I am currently using the following .htaccess rules
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteRule (.*) ./index.php?id=$1 [L]
</IfModule>
but they only work for urls like example.com/redirect/index.php?id=test if I try example.com/redirect/test I get a 404 error page.
It might be good to know, that I have 2 .htaccess files, one in my root directory and one in the root/redirects/ directory.
Best regards !
Try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([^/]+)?/?([^/]+)?/?([^/]+)?/? [NC]
I'm using this rewriterule:
RewriteRule ^page/([^/]*)$ http://example.com/page?q=$1 [QSA,L]
If I go to example.com/page/somePage I get redirected to example.com/page?q=somePage
But I don't want a redirection, what I want is the URL to always be example.com/page/somePage
How to do this?
Thank you
I removed http://example.com but it doesn't work, I get Page not Found.
I am using Wordpress for my site, this is my complete .htaccess:
RewriteEngine On
RewriteBase /
RewriteRule ^page/([^/]+)/?$ page?q=$1 [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
Whenever you specify the http:// at the beginning of the path to be rewritten to, Apache will always force a 301 redirect to the new URL, whether the URL is on the same website or not. Simply removing the http://example.com part should fix your problem.
As for the page not found, is there another RewriteRule somewhere that tells just 'page' to be processed as 'page.php' or something of the sort? Do you have your PHP files saved without extensions?
Well then your problem is you definitely need to remove the [L] flag because you're telling Apache not to process any more RewriteRules for that request, so it never looks at the WordPress rewrites because that rule was already executed and Apache was told that should be the final rule. I would recommend leaving the [QSA] in the line though, that would not affect the overall outcome of your script.