I need help with a redirect using htaccess since I moved my phpbb forum to a subfolder called "forum". So I want viewtopic.php?... to redirect to /forum/viewtopic.php?...
and viewforum.php?... to /forum/viewforum.php?...
I google all day and couldn't find an exact code to use so I tried to fiddle with the code to no avail.
RewriteCond %{REQUEST_URI} !forum/
RewriteRule ^([view(.+)\.php(.+)])$ forum/$1 [QSA,NC,L,R=301]
I tried to catch both "viewtopic" and "viewforum" and then redirect it to /forum/ but it's not working.
This should work for you:
RewriteCond %{REQUEST_URI} !^/forum/
RewriteRule ^view(.+)\.php$ /forum/$0 [QSA,NC,L,R]
Because of the nature of the redirect (with the need to capture the entire request URI), there is no need to wrap it - you can just us $0.
You were using square brackets in your capture, which which would not have helped in any way. Square brackets indicate a character set.
If the new rule works for you, change the R flag to R=301 (as you had it previously), which will make the redirect permanent.
Update: Your entire .htaccess file should look like this now:
Options All -Indexes
RewriteEngine On
RewriteRule ^$ - [E=noabort:1]
# Redirect Forum
RewriteCond %{REQUEST_URI} !^/forum/
RewriteRule ^view(.+)\.php$ /forum/$0 [QSA,NC,L,R]
# WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
The redirect rule must come before the WordPress rules.
Related
I have a problem removing folders from an url. I want that google / old links aren't broken. The old webpage had several sections with a structure like this
example.com/news/items/entry1.html
example.com/news/items/entry2.html
example.com/blog/items/foo.html
The new page has the urls like this:
example.com/news/entry1
example.com/news/entry2
example.com/blog/foo
Removing html was rather straight forward
<IfModule mod_rewrite.c>
RewriteEngine On
# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php [QSA,L]
RewriteCond %{THE_REQUEST} /([^.]+)\.html[\s?] [NC]
RewriteRule ^ /%1 [R=302,L,NE]
</IfModule>
The part I'm struggling with is removing the 'items' part. The rules I found only worked for request path like 'example.com/items/subfolder1/...'
Any help would be greatly appreciated.
To remove /items/ from your URLs you can use the following in your .htaccess file:
RewriteEngine On
RewriteRule ^(.*)/items /$1 [L,R=301]
So for example, this will take the URL: http://example.com/news/items/entry1 and turn it into http://example.com/news/entry1
Make sure you clear your cache before testing this.
i have a problem with an mod rewrite rule in the WOULD NOT WORK area :)
I have an Project site witch is just an ajax return site.
So i like to open my site on sitename.com/my-test/project/test.html and would have a rewrite to sitename.com/my-test/project.html?index=test.
I know after my rewrite the rewrite rules of the cms must going his work. Im not sure maybe this is the problem and they cross or crash.
If i browse the URLs via browser all will work just fine, only the rewrite rule would not work and i become total crazy and cant find the problem.
So i hope someone can help me.
REWRITE CONFIG in .htaccess
RewriteEngine On
RewriteBase /
#### WOULD NOT WORK ######
# REWRITE AJAX PROJEKT
RewriteCond %{REQUEST_URI}% ^my-test/projects/[a-zA-Z0-9]+\.html$ [NC]
RewriteRule ^my-test/projects/[a-zA-Z0-9]+\.html my-test/projects.html?index=$1
### WORK CORRECTLY ####
# REWRITE RULE FOR SEO FRIENDLY IMAGE MANAGER URLS
RewriteRule ^files[0-9]*/imagetypes/([^/]*)/([^/]*) index.php?rex_img_type=$1&rex_img_file=$2
# DON'T REWRITE IF REAL FILE, FOLDER OR SYMLINK EXISTS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
# EXCLUDE SPECIFIC FOLDERS FROM REWRITE ENGINE
RewriteCond %{REQUEST_URI} !/files[0-9]*/
RewriteCond %{REQUEST_URI} !/assets/.*
RewriteCond %{REQUEST_URI} !/media/.*
RewriteCond %{REQUEST_URI} !/redaxo/.*
# REWRITE ALL OTHER REQUESTS TO INDEX.PHP
RewriteRule ^(.*)$ index.php?%{QUERY_STRING} [L]
and i also have tested
RewriteCond %{REQUEST_URI}% ^/my-test/projects/[a-zA-Z0-9]+\.html$ [NC]
RewriteRule ^/my-test/projects/[a-zA-Z0-9]+\.html my-test/projects.html?index=$1
#OR JUST
RewriteRule ^/my-test/projects/[a-zA-Z0-9]+\.html$ my-test/projects.html?index=$1
#OR
RewriteRule ^/my-test/projects/(.*)$ my-test/projects.html?index=$1
#AND CORRECT
RewriteCond %{REQUEST_URI} ^/my-test/projects/[a-zA-Z0-9]+\.html$ [NC]
RewriteRule ^/my-test/projects/[a-zA-Z0-9]+\.html my-test/projects.html?index=$1
#AND ---
RewriteRule ^my-test/projects/([a-zA-Z0-9]+)\.html my-test/projects.html?index=$1 [L]
I dont find the answer, so i realy hope someone can help me
Thank you all
ng
moxx
RewriteCond %{REQUEST_URI}% ^my-test/projects/[a-zA-Z0-9]+\.html$ [NC]
RewriteRule ^my-test/projects/[a-zA-Z0-9]+\.html my-test/projects.html?index=$1
You have an extra % at the end of your condition: %{REQUEST_URI}%. That means it must be the request URI followed by a %, and you text on the right ends with html, which means this condition will always fail.
Additionally, the %{REQUEST_URI} variable starts with a /. Note that the leading slash is gone when used to match the pattern in the rewrite rule.
You actually don't need the condition at all.
RewriteRule ^my-test/projects/([a-zA-Z0-9]+)\.html my-test/projects.html?index=$1 [L]
I have limited .htaccess knowledge and i am requiring some help. I need to do some redirects to enable pretty urls.
In local all works fine but it is not working in another develpment server. apparently the query string get drop when redirect.
i need to redirect this http://mysite.local/info/permalink/1
to this one http://mysite.local/info?proxy=true&id=1
my .htaccess code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#remove /index.php from home url and redirect to root.
#http://mysite.local/index.php -> http://mysite.local/
RewriteCond %{THE_REQUEST} ^.*\/index\.php?\ HTTP/
RewriteRule ^(.*)index\.php?$ "/$1" [R=301,L,QSA]
#pretty url without index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [PT,QSA,L]
#rewrite to handle some permalink saved on my db.
#http://mysite.local/info/permalink/1
#http://mysite.local/info/proxy=true&id=1
RewriteRule ^([^/]+)/info/([a-zA-Z0-9\-]+)/([0-9]+) /info/?proxy=true&id=$3 [L]
</IfModule>
the redirect is working but the query string is not present. When I run var_dump($_GET) on info module i am getting an empty array array(0) {}
i have try it to solve changing
RewriteRule .* index.php [PT,QSA,L]
to RewriteRule ^(.*)$ /%{QUERY_STRING} [L]
and
RewriteRule ^([^/]+)/info/([a-zA-Z0-9\-]+)/([0-9]+) /info/?proxy=true&idobj=$3 [L]
to
RewriteRule ^([^/]+)/info/([a-zA-Z0-9\-]+)/([0-9]+) /info/?proxy=true&idobj=$3 [QSA,NE,L]
Server API is CGI/FastCGI
What should I change to ensure that my rewrite works as intended and $_GET variables still are accessible?
thx in advance
I have no idea how you've managed to get this to work with a regex pattern like: ^([^/]+)/info if the URL you are going to is /info/permalink/1.
The ^([^/]+)/info pattern means there's stuff before the /info part of the URI, which there isn't. Additionally, in an htaccess file, the URI's have the leading forward slash stripped off. So you probably want something like this:
RewriteRule ^info/([a-zA-Z0-9\-]+)/([0-9]+) /info/?proxy=true&id=$2 [L]
Trying to figure out how to write my httaccess to let me have it so my urls are 'www' free and allow me to have a slug style setup for 'friendly' urls. I seem to keep writing myself into an internal 500 error though. That or it doesn't seem to carry over the extra stuff. extra stuf being anything from .com/ over ie mydomain.com/hello/world
RewriteEngine On
Options +FollowSymlinks -MultiViews
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.mydomain.com [NC]
RewriteRule ^(.*)/?$ http://mydomain.com/index.php?r=$1
Overall goal take URL in either of these 2 fashions
http://mydomain.com/hello/world
http://www.mydomain.com/hello/world
and have it translate to
http://mydomain.com/hello/world to the front end but on the backend be the equivalent to http://mydomain.com/index.php?r=hello/world
also I would like to apply conditions where if a file exists, or folder exists, or whatever exists stop the rewrite cold. I know this is possible well without the removal of the www part, not sure with that part, as I used to once have an htaccess file that I could do this with, but I have lost that file and its been far to long since ive played with htaccess to remember how i did it in the first place.
This will forward www to non-www site. Also take care of the index.php.
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
RewriteRule ^(.*)$ index.php?r=$1 [QSA,L]
You can also add the following two lines. Like this if you have any css or images it will not rewrite them to index.php:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?r=$1 [QSA,L]
This is not tested but a general guidance.
My URL structure is currently as follows:
http://domain.com/folder/filename (CURRENT)
I want to change this so that I can use the following URL instead:
http://sub.domain.com/filename (NEW)
So accessing the CURRENT or the NEW url, should load the file located at the CURRENT url, but show the NEW url in the address bar. It should only apply to the "/folder/" path.
sub.domain.com is a mirror of domain.com, ie. they share the same file system and root directory.
This is what I have so far:
Options +FollowSymLinks
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteCond %{REQUEST_URI} ^/folder/?(.*)$ [NC]
RewriteRule ^(.*)$ http://sub.domain.com/$1 [R=301,L]
This is working, but is missing the rule to remove the "/folder/" from the path. I've tried combining multiple RewriteRule's with no luck. Any ideas? Thanks.
UPDATE: Thanks again #Gerben - I understand what your rules are doing now, but the second one isn't working for me. I suspect because it's conflicting with some other rewrite rules, in particular those of WordPress, which are lower down in my .htaccess file:
# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
Because of this the page ends up in a redirect loop, ie (from Chrome):
"The webpage at http://sub.domain.com/folder/index.php has resulted in too many redirects." - while the url I was originally trying to access was, for example, http://sub.domain.com/page
Any ideas?
Try:
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteRule ^(folder/)?(.*)$ http://sub.domain.com/$2 [R=301,L]
This will redirect everything to sub.domain.com, and remove the /folder part of the URI if it is there. If not, it redirects and leaves the URI untouched.
RewriteCond %{THE_REQUEST} /folder/
RewriteRule ^folder/(.*)$ http://sub.domain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule ^(.*)$ folder/$1 [L]
# WordPress rules here
edit the second R=301 should not have been there
But this won't work, as wordpress has no way of knowing you want folder. You could add the Proxy flag to the rewrite, but then you need to change the rule above to not redirect on this internal proxy request.