My current .htaccess file looks like this:
RewriteEngine on
RewriteBase /
Options +FollowSymLinks -Indexes
RewriteRule ^video/(.*)$ video.php?id=$1 [L]
RewriteRule ^video/(.*)$([a-zA-Z0-9]+) video.php?id=$1 [L]
RewriteRule ^tag/(.*)/page-(.*)/$ tag.php?tag=$1&page=$2 [L]
RewriteRule ^tag/(.*)/page-(.*)$ tag.php?tag=$1&page=$2 [L]
RewriteRule ^tag/(.*)?$ tag.php?tag=$1
RewriteRule ^page/(.*)$ page.php?id=$1 [L]
RewriteRule ^feed feed.php [L]
i want to add a slash to all my url's
like this:
> example.com/video/video_id/
>
> example.com/tag/keyword/
>
> example.com/tag/keyword/page-2/ (3... and so on...)
>
> example.com/page/name/
>
> example.com/feed/
And i want to redirect my current links to the new slash url's
Can somebody help me, please?
Your current htaccess file supports a trailing / although you probably would prefer
RewriteRule ^video/(.*)/$ video.php?id=$1 [L]
So that you don't have to handle the / in video.php
Just update all of your URLs to be example.com/video/video_id/ instead of example.com/video/video_id in whatever you are using (your framework/flat HTML files).
Your old URLs will still work. If you really want to redirect them, you can:
RewriteCond %{REQUEST_URI} ^video/ [NC]
RewriteRule ^video/(.*)$ video.php?id=$1 [L,R=301]
The [NC] means No-case checking (so /VIDEO) would work. The [R=301] means permanent redirect (useful for SEO).
Go through and expand for your other rules.
Edit:
Sorry, I don't think it was quite right before. Try the following:
RewriteEngine on
RewriteBase /
Options +FollowSymLinks -Indexes
RewriteCond %{REQUEST_URI} ^video/ [NC]
RewriteRule ^video/(.*)$ video/$1/ [L,R=301]
RewriteRule ^video/(.*)/$ video.php?id=$1 [L]
RewriteCond %{REQUEST_URI} ^tag/ [NC]
RewriteRule ^tag/(.*)/page-(.*)$ tag/$1/page-$2/ [L,R=301]
RewriteRule ^tag/(.*)/page-(.*)/$ tag.php?tag=$1&page=$2 [L]
...
Related
I have some old URL's that I want to fix because of a forum migration.
The old URL's look like:
http://www.example.com/forum/topic.asp?TOPIC_ID=666
I want to redirect them to:
http://www.example.com/forum/missions/666
My approach is this, but I'm scratching my head, because it doesn't work at all:
RewriteCond %{QUERY_STRING} ^TOPIC_ID=(.*)$ [NC]
RewriteRule ^/forum$ /forum/missions/%1 [NC,L,R=301]
Assuming there is no .htaccess in `/forum/, you can use this first rule in your root .htaccess:
RewriteCond %{QUERY_STRING} ^TOPIC_ID=([^&]+) [NC]
RewriteRule ^forum/topic\.asp$ /forum/missions/%1? [NC,L,R=302]
If there is a .htaccess in /forum/, then you can use this first rule in your /forum/.htaccess:
RewriteCond %{QUERY_STRING} ^TOPIC_ID=([^&]+) [NC]
RewriteRule ^topic\.asp$ /forum/missions/%1? [NC,L,R=302]
I'd suggest this, but cannot really try from here :)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^forum/topic.asp\?TOPIC_ID=([0-9]+)$ forum/missions/$1 [L]
</IfModule>
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 =)
I have a directory structure like this
/ub/
/ub/img/
/ub/inc/
Basically I want to change my img URL to it's parent directory (ub),
When i access http://localhost/ub/mypic.jpg
First, it has to check /ub/img/mypic.jpg, if exist then get that file and pass to the browser, if not then just pass it to http://localhost/ub/index.php?img=mypic.jpg with INTERNAL REDIRECTION.
Second, index.php will create mypic.jpg and store it to /ub/img/, then pass mypic.jpg to the browser.
So, whether the file is exist or not, the browser will only have http://localhost/ub/mypic.jpg as the URL
So far, i have this on my .htaccess file
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/ub/img/$1 -f
RewriteRule .+ /ub/img/$1 [L]
I have 2 days to find out the solution, but, it doesn't seem to work,
Thanks,
EDIT :
Hey, I've got this, The request to http://localhost/ub/app/file.php is already passed to index.php?uri=app/file.php as i expected,
but the request to http://localhost/ub/img/mypic.jpg is not passed to index.php?uri=img/mypic.jpg. Is there any solution?
Options -MultiViews +FollowSymLinks
RewriteEngine On
RewriteBase /ub/
RewriteCond %{REQUEST_URI} ([^/]+)$
RewriteCond %{DOCUMENT_ROOT}ub/img/%1 -f
RewriteRule ^(.+)$ img/%1 [L]
RewriteCond %{REQUEST_URI} ([^/]+)$
RewriteCond %{DOCUMENT_ROOT}ub/inc/%1 -f
RewriteRule ^(.+)$ inc/%1 [L]
RewriteRule ^(.+)$ index.php?uri=$1 [QSA,L]
Try something like this (place it in .htaccess in the /ub folder):
Options +FollowSymLinks
RewriteEngine on
RewriteBase /ub/
RewriteCond %{REQUEST_URI} ([^/]+\.jpg)$
RewriteCond %{DOCUMENT_ROOT}/ub/img/%1 -f
RewriteRule .+ - [L]
RewriteRule ^(.*)$ index.php?img=$1
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.]
For my site I have a directory called /test/. I want to rewrite www.example.com/nl/test and www.example.com/nl/test/ to a certain page (test.php).
Some global conditions (for all the rules)
RewriteRule ^(nl|en)$ http://www.example.com/$1/ [NC,R]
RewriteBase /
RewriteRule ^(nl|en)$ $1/ [NC,R]
RewriteCond $1 !^(en|nl)$
RewriteRule ^([a-z]{2})/(.*)$ en/$2 [L,R=302]
RewriteRule ^(nl|en)/(.*)$ $2?language=$1&%{QUERY_STRING} [L]
RewriteRule ^sale$ sale.php
RewriteRule ^valentine$ valentine.php
Some conditions for the rewrite + folder
RewriteRule ^test/$ test.php
The redirect of www.example.com/nl/test/ is correct. The language parameter is also correctly rewritten.
For the second redirect (the version without the trailing slash) I can't get this working.
RewriteRule ^test$ test.php
Now my URL is rewritten as www.example.com/test/?language=nl
Can someone give me a tip or hint to fix this? I can't change the name of the directory since there are several external URLs linking to this directory.
This rule will do the whole job (instead of 4 lines you have there): it will rewrite both /nl/test and /nl/test/ to /test.php?language=nl.
RewriteRule ^(en|nl)/test/?$ /test.php?language=$1 [NC,QSA,L]
NOTES:
The [QSA] flag will preserve any existing query string (therefore, there is no need for &%{QUERY_STRING}).
Full .htaccess:
Options +FollowSymLinks -MultiViews
DirectorySlash Off
RewriteEngine On
RewriteBase /
RewriteRule ^(nl|en)$ http://www.example.com/$1/ [NC,R=301,L]
RewriteCond $1 !^(en|nl)$
RewriteRule ^([a-z]{2})/(.*)$ /en/$2 [R=302,L]
RewriteRule ^(nl|en)/(.*)$ /$2?language=$1 [NC,QSA,L]
RewriteRule ^sale/?$ sale.php [QSA,L]
RewriteRule ^valentine/?$ valentine.php [QSA,L]
RewriteRule ^test/?$ test.php [QSA,L]
NOTES:
There is no need for RewriteRule ^(nl|en)$ $1/ [NC,R] as you already have RewriteRule ^(nl|en)$ http://www.example.com/$1/ [NC,R=301,L]. It does the same job.