I have a Forum on my site and need to redirect weirdly generated urls.
Every url contains ?id=, eg:
https://www.example.com/forum/topic/casualthread/page/25?id=casualthread
and I need to remove the ?id= and everything that follows in order to have:
https://www.example.com/forum/topic/casualthread/page/25
I am trying to modify this code I found here on Stackoverflow with very scarce results:
RewriteEngine On
RewriteBase /
# Make sure there is a query string
RewriteCond %{QUERY_STRING} .
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /?id= [R=301,L]
The htaccess file i am editing is in the forum directory:
https://www.example.com/forum/
and it redirects everything to the homepage https://www.example.com: what am I doing wrong?
You can use this
RewriteEngine on
RewriteCond %{QUERY_STRING} id=
RewriteRule ^ %{REQUEST_URI}? [L,R]
Since not just id, but also some other parameter(s) may be present in the query string, use:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^(.*)(^id=[^&]*&?|&id=[^&]*)(.*)$
RewriteRule ^(.+) /$1?%1%3 [R=301,L]
Related
I have an application on Codeigniter(3.1.11). Basically, I want a 404 redirect of URLs which have dashboard in URI. Like these:
dashboard/foo/bar
dashboard/foo-2/bar-2
dashboard/something
...
Also, I want to keep some exceptions in redirect rule so, some of the specific URLs which have dashboard as path URI should be excluded from this redirect. Let's say I want to exclude some URLs like:
dashboard/new-foo/new-bar
dashboard/one-thing/abc
dashboard/another-thing/xyz
I have given a few tries but the exclude rule is not working. It redirects all URLs to 404. This is what I have in my .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/(dashboard/new-foo/new-bar) [NC] # Exclude this url (this condition is not working)
RewriteRule ^(.*)$ dashboard/$1 [R=404,L]
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
It can be done in a single rule with negative lookahead conditions like this:
RewriteEngine on
RewriteRule ^dashboard(?!/new-foo/new-bar|/one-thing/abc|/another-thing/xyz) - [R=404,NC,L]
RewriteCond $1 !^(index\.php|resources|robots\.txt) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I'm not expert with htaccess but I am pretty sure you need 2 RewriteCond lines, one to include all the dashboard urls and then the second to exclude the new ones. Each RewriteCond is implicitly joined with an "AND" so if you have many different patterns to exclude and you need a 3rd RewriteCode then you will need to join the 3rd with an "OR" on the 2nd condition.
e.g.
RewriteCond %{REQUEST_URI} ^/dashboard/(.*) [NC]
RewriteCond %{REQUEST_URI} !^/dashboard/new-(.*) [NC]
RewriteRule ^(.*)$ 404-page/$1 [R=404,L]
There are a couple of other things I'd like to mention: 1) Your RewriteRule redirects back to a /dashboard URL so you could possibly end up in a continuous look here. 2) You don't need to turn on Rewrite engine twice, once at the top is enough.
If the rewrite rules in htaccess are getting complicated then perhaps you could use your index.php file to handle it (or some other method within Codeigniter).
i am trying to redirect thoses urls ( from a phphbb )
http://www.example.com/fraiseuses/restauration-gambin-10n-t33924-45.html
http://www.example.com/fraiseuses/une-gambin-10n-plus-sur-forum-t34642.html#p675680
to a the correct xenforo url
http://www.example.com/threads/restauration-gambin-10n.33924/page-3
http://www.example.com/threads/34642/page-1#post-675680
my htacces
RewriteEngine On
RewriteRule ^post([0-9]+).html$ /posts/$1 [R=301,L]
RewriteRule ^.*-f([0-9]+)$ /forums/$1 [R=301,L]
RewriteRule ^[^/]+/topic([0-9]+).html /threads/$1 [R=301,L]
RewriteRule ^.*-t([0-9]+).html /threads/$1 [R=301,L]
RewriteRule ^[^/]+/[^\./]+-t([0-9]+)-15\.html$ /threads/$1/page-1 [R=301,L]
RewriteRule ^[^/]+/[^\./]+-t([0-9]+)-30\.html$ /threads/$1/page-2 [R=301,L]
RewriteRule ^[^/]+/[^\./]+-t([0-9]+)-45\.html$ /threads/$1/page-3 [R=301,L]
Unfortunately i can't find my mistake, can somebody point out my mistake please? Thanks.
There are no phpBB-specific redirect scripts, but these will work::
http://xenforo.com/community/threads/redirection-scripts-for-vbulletin-3-x.5030/
The basic function of these scripts is to map the old ids to the new ones. They can work with phpBB URLs given appropriate rewrite rules. Here are steps for you:
1) Upload these redirect scripts to your old forum location (the /forum folder):
http://xenforo.com/community/threads/redirection-scripts-for-vbulletin-3-x.5030/
You only need the 301config.php file and the showthread.php file to redirect threads (which are the most important thing).
2) Edit the 301config.php file. Uncomment this line (remove the //) and specify the path to your XF directory:
// $fileDir = '/home/username/www/forums';
You may also need to specify the name of the import log table if it's not the default (xf_import_log). It may be named archived_import_log:
// define('IMPORT_LOG_TABLE', 'import_log_x');
3) Add these rewrite rules to your .htaccess file in the /forum directory. This is based on previous phpBB imports. I assume your URLs follow the same format:
RewriteEngine on
RewriteCond %{QUERY_STRING} (^|&)t=([0-9]+)(&|$)
RewriteRule ^viewtopic\.php$ /forum/showthread.php?t=%2 [L,R=301,NC]
thx
i did this :
RewriteCond %{QUERY_STRING} (^|&)t=([0-9]+)(&|$) [NC]
RewriteRule ^viewtopic\.php$ /threads/%2? [L,R=301,NC]
RewriteCond %{QUERY_STRING} (^|&)p=([0-9]+)(&|$) [NC]
RewriteRule ^viewtopic\.php$ /posts/%2? [L,R=301,NC]
RewriteCond %{QUERY_STRING} f=(\d+)$ [NC]
RewriteRule ^viewforum\.php$ /forums/%1 [L,R=301,NC]
RewriteRule ^viewforum\.php$ /forums/%1? [L,R=301,NC]
i need to redirect the URL as
www.domain.com/?page=news --> www.domain.com/news
Here my htaccess file:
Options -Multiviews
RewriteEngine On
RewriteBase /
# Force search engines to use www.domain.com
RewriteCond %{HTTP_HOST} !^www\.domain\.com$
RewriteRule ^(.*) http://www.domain.com/$1 [R=301,L]
# Specify search friendly URLs
RewriteRule ^http://www\.domain\.com/news/$ /http://www.domain.com/?page=news [L]
Please suggest me the exact rule to use in .htaccess file.
Thanks in advance.
Your 2nd rule is not correct as you can't match domain name in RewriteRule pattern. That pattern only matches REQUEST_URI without domain name and query string.
Your 2nd rule should be like this:
RewriteCond %{THE_REQUEST} \s/+\?page=([^&\s]+) [NC]
RewriteRule ^ /%1? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# Specify search friendly URLs
RewriteCond %{QUERY_STRING} !(^|&)page=[^&]+
RewriteRule ^([^/]+)/?$ /?page=$1 [L,NC,QSA]
Reference: Apache mod_rewrite Introduction
Sorry, I do not know a thing about this, but I have seen a good tutorial posted by someone I am following on facebook:
http://www.9lessons.info/2013/11/htaccess-file-tutorial-and-tips.html
Hope this helps.
To note, I've found similar questions on StackOverflow but they have not worked as I need.
I have a URL such as:
http://www.example.com/index.php/test
I'd like to remove the index.php directory, so if the above is entered it would go to:
http://www.example.com/test
This appears to work
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,L]
However if the url is:
http://www.example.com/index.php?option=example
It turns into
http://www.example.com/?option=example
So index.php should only be removed if it's a directory like in my first example.
Also if you type in for instance:
http://www.test.example.com/index.php/index.php/dfd
It should go to
http://www.test.example.com/dfd
the rules below will:
not apply for /index.php?o=a
redirect /index.php/index.php/dfd to /dfd
redirect /index.php/index.php/dfd?a=b to /dfd?a=b
redirect /index.php/index.php?a=b to /index.php?a=b
.
RewriteCond %{QUERY_STRING} .+
RewriteRule ^index\.php(/index\.php)+/?$ /index.php [R=302,L,QSA,NC]
RewriteCond %{QUERY_STRING} ^$ [OR]
RewriteCond %{REQUEST_URI} !index\.php/?$
RewriteRule ((^|/)index\.php)+/?(.*)$ /$3 [R=302,L,QSA,NC]
The current RewriteRule removes any query except query callback for any URL.
# Remove question mark and parameters
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^?#\ ]*)\?[^\ ]*\ HTTP/ [NC]
# Query rewrite exceptions
##RewriteCond %{REQUEST_URI}?%{QUERY_STRING} !^/api.*?callback=.* #does not work
RewriteCond %{QUERY_STRING} !callback=
RewriteRule .*$ %{REQUEST_URI}? [R=301,L]
How to avoid query callback rewrite just from URL ^api\/?([^\/]*)$? Excepted result:
no rewrite for /api?callback=1, /api/user?callback=1, /api/user/2?callback=1
rewrite for /apis?callback=1, /user?callback=1, /api/user?foo=1 etc.
I finally understood your question...
Replace these lines:
##RewriteCond %{REQUEST_URI}?%{QUERY_STRING} !^/api.*?callback=.* #does not work
RewriteCond %{QUERY_STRING} !callback=
with this line:
RewriteCond %{REQUEST_URI}?%{QUERY_STRING} !^/api(/.*)?\?callback=.*
Important notice:
if your script isn't located in the document root, but, i.e., in dir /htest,
and full URL looks like mine: http://localhost/htest/api/?callback=1, then you have to put full path to API in your RewriteCond:
RewriteCond %{REQUEST_URI}?%{QUERY_STRING} !^/htest/api(/.*)?\?callback=.*
You can overcome that by beginning your regex with !/api instead of ^/path/to/api, but /foo/api and /bar/api will be skipped from rewriting too.
Update:
this .htaccess works fine in document root:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^?#\ ]*)\?[^\ ]*\ HTTP/ [NC]
RewriteCond %{REQUEST_URI}?%{QUERY_STRING} !^/api(/.*)?\?callback=.*
RewriteRule .*$ %{REQUEST_URI}? [R=temporary,L]
you may try using it without any other rules to check what is wrong
Update 2
If you have other condition, i.e.,
RewriteRule ^([^.]*)$ index.php?url=$1 [QSA,L]
repeat RewriteCond before it:
RewriteCond %{REQUEST_URI}?%{QUERY_STRING} !^/api(/.*)?\?callback=.*
RewriteRule ^([^.]*)$ index.php?url=$1 [QSA,L]
also to be able to use these rules in /foo subdir, replace ^/api with ^/foo/api
To enable RewriteRule for index.php, need to add in query rewrite exceptions.
This rules works fine and fixes this issue:
# Remove question mark and parameters
RewriteCond %{QUERY_STRING} .+
# Query rewrite exceptions
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteCond %{REQUEST_URI}?%{QUERY_STRING} !/api(/.*)?\?callback=.*
RewriteRule .*$ %{REQUEST_URI}? [R=301,L]