I have the following url
http://localhost/mybb/index.php?url=widgetboard/questions&step=1&id=1
which turns into the following url after applying rewrite rule
http://localhost/mybb/widgetboard/questions/step/1/id/1
Is it possible to remove the 2 words ie "step" and "id" from the url and add .html at the very end of the url
my code is
Options -MultiViews
# turn rewriting on
RewriteEngine On
# When using the script within a sub-folder, put this path here, like /mysubfolder/
# If your app is in the root of your web folder, then please delete this line or comment it out
RewriteBase /mybb/
RewriteRule ^(.+?)/(step)/([0-9]+)/(id)/([0-9]+)/?$ index.php?url=$1&$2=$3&$4=$5 [NC,L,QSA]
RewriteRule ^(.+?)/(category)/([0-9]+)/(answer)/([0-9]+)/?$ index.php?url=$1&$2=$3&$4=$5 [NC,L,QSA]
RewriteRule ^(.+?)/(step)/([0-9]+)/?$ index.php?url=$1&$2=$3 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?url=$1&$2=$3 [NC,L,QSA]
You can insert this rule just below RewriteBase /mybb/ line:
RewriteRule ^([^/]+)/([^/]+)/([0-9]+)/([0-9]+)/?$ index.php?url=$1/$2&step=$3&id=$4 [L,QSA]
Change your second rule to:
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?step=$1&id=$2 [NC,L,QSA]
Then if a user clicks this url: domain.com/3/120 your PHP script will receive index.php?step=3&id=120
Related
All the examples I see with RewriteRule are something like RewriteRule ^([a-z])$ index.php?example=$1
But I have the following case: I want the
example.com/en/something
to rewrite to
example.com/something?lang=en
And that applies to all things in the place of something.
Basically I want to only take the first parameter if it's en or ru and rewrite to the same link but only to pass it as a get parameter
So
example.com/en/something-else/anything
also rewrites to
example.com/something-esle/anything?lang=en
How can I do that ?
UPDATE
I tried this RewriteRule ^(en|ru)/(.*)$ $2?lang=$1 but it says that the page is not found (it returns the 404 page from my application not from the server).
I managed to fix it after all.
This is my .htaccess now:
# SEO URL Settings
RewriteEngine On
# If your opencart installation does not run on the main web folder make sure you folder it does run in ie. / becomes /shop/
RewriteBase /avc0135/
#Language
RewriteRule ^(en|ru|bg)/(.*)$ $2?lang=$1
RewriteRule ^(en|ru|bg)$ ?lang=$1
#Language
RewriteRule ^sitemap.xml$ index.php?route=extension/feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=extension/feed/google_base [L]
RewriteRule ^system/storage/(.*) index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
After a few searches on how exactly does .htaccess works I managed to figure out the RewriteRule, the problem after that was that the #Language block was at the bottom, I moved before everything else and it worked.
I am currently using this .htaccess rewrite to get pretty urls for my blog posts:
RewriteRule ^([a-zA-Z0-9-]+)$ /item.php?slug=$1 [L]
This ends up looking like:
example.com/my-new-awesome-blog-post
What I would like is to move all my posts to a new folder called items so that it would look like:
example.com/items/my-new-awesome-blog-post
I not only want to move all blog posts to the subfolder, but also to ensure it is a proper 301 redirect so there is no issue with SEO.
Thank you for your help/advice.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
Options -MultiViews
RewriteEngine On
RewriteBase /
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} !\s/+items/ [NC]
RewriteRule ^ items%{REQUEST_URI} [L,NE,R=302]
RewriteRule ^items/([a-zA-Z0-9-]+)/?$ items/item.php?slug=$1 [L,QSA]
I want to redirect URLs of this form:
/page.html?variable=value&othervar=true&thirdvar=100
To this:
/page/?variable=value&othervar=true&thirdvar=100
So basically I just want to replace the .html in the middle of the URL with a forward slash, but I need to preserve the get string that comes with it. This is what I tried:
RewriteRule ^page.html(.+)$ /page/$1 [L,R=301]
But this doesn't appear to be working for me. I've made similar things work recently but I can't figure out what I'm missing here. Thanks for any input.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# external redirect from /example.html to /example
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^.]+)\.html [NC]
RewriteRule ^ /%1/ [R=301,L]
# internal forward from /example/ to //example.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+?)/?$ /$1.html [L]
I have the following in my htaccess file:
RewriteEngine On
RewriteBase /
# Check to see if the URL points to a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Trailing slash check
RewriteCond %{REQUEST_URI} !(.*)/$
# Add slash if missing & redirect
RewriteRule ^(.*)$ $1/ [L,R=301]
# Check to see if the URL points to a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Send to index.php for clean URLs
RewriteRule ^(.*)$ index.php?/$1 [L]
This does work. It hides index.php, and it adds a trailing slash... except when there is a query string.
This URL:
http://example.com/some-page
gets redirected to:
http://example.com/some-page/
but this URL:
http://example.com/some-page?some-var=foo&some-other-var=bar
does not get redirected. I would like for the above to be sent to:
http://example.com/some-page/?some-var=foo&some-other-var=bar
I've reached the limits of my understanding of redirects with this. If you have a working answer, I would really appreciate a walkthrough of what every line is doing and why it works. Double bonus awesomeness for an explanation of why what I have right now doesn't work when there is a query string involved.
Try adding a [QSA] to the end of the last Redirect rule to preserve the original query string as below
# Send to index.php for clean URLs, preserve original query string
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
a walkthrough of what every line is doing and why it works.
See my comments below
#turn mod_rewrite engine on.
RewriteEngine On
#set the base for urls here to /
RewriteBase /
### if the is not a request for an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
### and the URI does not end with a /
RewriteCond %{REQUEST_URI} !(.*)/$
### redirect and add the slash.
RewriteRule ^(.*)$ $1/ [L,R=301]
### if the is not a request for an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite to index.php passing the URI as a path, QSA will preserve the existing query string
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
I believe that if you change this:
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]
to this:
RewriteCond %{REQUEST_URI} !^([^?]*)/($|\?)
RewriteRule ^([^?]*) $1/ [L,R=301]
then it should do what you want.
The changes I made are:
In both rewrite-condition and -rule, I changed (.*) and ^(.*) to ^([^?]*), to ensure that, if there's a query-string, then it is not included in either regex. ([^…] means "any character that is not in …", so [^?] means "any character that is not a question mark".)
In the rewrite-condition, I changed $ to ($|\?), so as to match either end-of-URL or end-of-part-before-the-query-string.
In the rewrite-rule, I dropped the $, since it was no longer needed.
I'm having trouble with this reg expression which i belive is correct, but it is not working.
What im trying to do is redirect bunch of urls containing a specific string like this:
http://www.example.com/**undesired-string**_another-string to http://www.example.com/**new-string**_another-string
and
http://www.example.com/folder/**undesired-string**/another-string to http://www.example.com/folder/**new-string**/another-string
So i have this code in the .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule (.+)+(undesired-string)+(.+) $1new-string$2 [R=301,L]
</IfModule>
This should replace ANY undesired-string in any url to new-string, but it is not working, any idea why ?
Thank you
Marwen: Try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)undesired-string(.*)$ yoursite.com/$1new-string$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{HTTP_HOST} !^www.yoursite.com$ [NC]
RewriteRule ^(.*)$ yoursite.com//$1 [L,R=301]
</IfModule>
In your 'updated' code in the comments above, you had it applying the rewrite condition to the undesired-string... So if the actual file or directory was valid it would not rewrite...
Doing this though will always rewrite the undesired-string with new-string - even if its a file name... If that is fine or what you want then all you had to do was move your rewrite conditions to below the rewrite rule...
also.. Just an fyi.. If everything is on yoursite.com you dont need to list yoursite.com
i.e.
yoursite.com/$1new-string$2
just needs to be
/$1new-string$2
which does the same thing: rewrites to the base directory of yoursite.com
now if they are going from mysite.com to yoursite.com then you woulud want to include the domain name because you are redirecting across domain names
Edit: You may also want to use:
[QSA,L,R=301]
instead of just [L,R=301]
Your regex is not really correct. Try:
RewriteRule ^(.*)undesired-string(.*)$ $1new-string$2 [R=301,L]
Or if this doesn't work, try:
RewriteRule ^(.*)undesired-string(.*)$ http://yoursite.com/$1new-string$2 [R=301,L]
Explanation:
^ marks the beginning; $ marks the end; the first (..) goes to $1, the second (..) goes to $2 and so on; * is 0 or more chars; + is 1 or more chars.
To answer my own question. Laravel already redirects the trailing slashes. Problem was that Laravel was installed into a sub-directory. I added the location of the sub-directory to the redirect. My location in this case is: "/lumen/public/". See the fixed htaccess below.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /lumen/public/$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>