I have a site which uses a custom query string to deliver pages to the visitor. A sample query string looks like /?sec=news&pg=current&bg=203. I'd like to use .htaccess to rewrite the strings into a pattern like /news/current/203.
I know how to take the /news/current/203 URL and make it into a query string that I can parse with PHP. I typically use this bit of htaccess code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L]
RewriteRule ^(.*) /index.php?id=$1 [L]```
And my PHP then explodes id on the / and off we are running.
My question is this... How can I add a few more lines to this htaccess code that will allow the old URL query pattern to continue to work for people that have bookmarks or links that use the /?sec=news... pattern? Basically, it seems like I need to take this old query string and combine the values into one string that I can pass to index.php in the id parameter. I don't want to lose the ability to honor the old pattern, but also need to promote the new cleaner path-based string.
I know there is some regex that can help here, but I am terrible at understanding regular expressions. Any help would be appreciated, and let me know if I am not making any sense.
Update: The answer provided makes my final htaccess file look like this...
RewriteEngine on
# Prevent directory listings
Options All -Indexes
# Rewrite to www
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^sample.com[nc]
RewriteRule ^(.*)$ http://www.sample.com/$1 [r=301,nc]
# WHEN DONE TESTING< CHANGE ALL 302 to 301 !!!
# Honor the old format /index.php?sec=XXX&pg=XXX&bg=XXX and turn it into the new format
RewriteCond %{QUERY_STRING} sec=(\w+)&pg=(\w+)&bg=(\d+)
RewriteRule ^(.*)$ /%1/%2/%3? [R=302,L]
# Turn /path/to/page into index.php?id=path/to/page
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L]
RewriteRule ^(.*) /index.php?id=$1 [L]
Add this rule
RewriteCond %{QUERY_STRING} sec=(\w+)&pg=(\w+)&bg=(\d+)
RewriteRule ^(.*)$ /%1/%2/%3? [R=302,L]
Change 302 to 301
Related
I currently use $_GET['base'] to determine which homepage that the user visits.
This results in localhost/?base=administrator or localhost/?base=guest
I am also using this to control which page is the user at, such as
localhost/?base=guest&page=register
Is there any way to use mod_rewrite, or htaccess, to change how this system works?
Modifying my code is not an issue, is this possible?
EDIT:
I am trying to achive this:
localhost/?base=guest to localhost/guest
localhost/?base=admin to localhost/admin
localhost/?base=guest&page=register to localhost/guest/register
Below is my htaccess file
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /?base=$1&page=$2 [L]
RewriteRule ^([^/]*)$ /?base=$1 [L]
Will the document path affect how it is being called? As I am using a case loop to include which items are needed.
This, however, works for localhost, but it will loop every other address to main.
RewriteEngine On
RewriteRule ^$ /index.php?base=guest[L]
But did not give a result as expected.
Your rules in .htaccess need to be in reverse order, like below:
RewriteRule ^([^/]*)/([^/]*)$ /?base=$1&page=$2 [L]
RewriteRule ^([^/]*)$ /?base=$1 [L]
That is because if it is kept in the order you have it, both localhost/?base=guest&page=register & localhost/?base=administrator will match the rule RewriteRule ^([^/]*)$ /?base=$1.
Having them in reverse order ensures that the first rule is matched only for localhost/?base=guest&page=register. It won't match the first rule for localhost/?base=administrator. I hope that helps.
You need to exclude your existent files and folders from the rule
RewriteEngine On
# if the request is a dir
RewriteCond %{REQUEST_FILENAME} -d [OR]
# or file
RewriteCond %{REQUEST_FILENAME} -f
#do nothing
RewriteRule ^ - [L]
RewriteRule ^([^/]*)/([^/]*)$ /?base=$1&page=$2 [L]
RewriteRule ^([^/]*)$ /?base=$1 [L]
So you can use this simple code:
RewriteEngine on
RewriteRule ^(\w+)$ index.php?base=$1 [L]
RewriteRule ^(\w+)/(\w+)$ index.php?base=$1&page=$2 [L]
\w will match symbols a-z, 0-9 and underscore _, I think those characters are enough for your case, but if you need expansion it will be easy
Also in this case you don't need to change your code, because you still get base and page parameters in the $_GET array
UPDATE:
to disable query string params page and base (other params may be needed) add these two lines to the code at the bottom:
RewriteCond %{THE_REQUEST} (\?|&)(page|base) [NC]
RewriteRule .* - [L,R=404]
I have written this little piece of code. I am very new to this so i am not sure it is all correct. but basically it lets me access urls with the php extension. When people get on the site they are being redirected from the geo ip page to the correct language which like looks like this
main.php?lang=uk or nl or en or eu etc.
right now i can also use it like this
main/?lang=uk or nl or en or eu etc.
I would like to be able to to also remove the variable in the url ?lang=uk.
How would i do this. My .htaccess code is below
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
DirectorySlash On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1 [R=301,L]
# remove trailing slash
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
RewriteRule ^([\w\/-]+)(\?.*)?$ $1.php$2 [L,T=application/x-httpd-php]
</IfModule>
Thanks too anyone willing to help.
The first argument of RewriteRule does match anything after the domain name and prefix* and before any query string if that exists. In http://localhost/this/is/my/test?lang=123 with a .htaccess file in the this/is/ directory it would match my/test. To match a query string, you have to use the %{QUERY_STRING} variable.
If the second argument (the rewritten url) of RewriteRule does not contain a query string, it will automatically append the query string of the original url to the new url.
In the code below I use %{THE_REQUEST}. This is the string that is used to make the request for a page. It is in the form of GET /my/resource?query=string HTTP/1.1. It does not change when rewriting things, which makes it useful to prevent infinite loops.
In your php file make sure that the language is read from a cookie instead of a get variable, then do the following:
#Set cookie for language
RewriteCond %{QUERY_STRING} ^(.*?)&?lang=([^&]+)&?(.*?)$
RewriteRule ^ %{REQUEST_URI}?%1&%3 [CO=lang:%2:127.0.0.1:1:/:0:1,R,L]
#Remove potential prefix or suffix & from query string
RewriteCond %{QUERY_STRING} ^&(.*?)$ [OR]
RewriteCond %{QUERY_STRING} ^(.*?)&$
RewriteRule ^ %{REQUEST_URI}?%1 [R,L]
#External requests with \.php should be without that.
RewriteCond %{THE_REQUEST} \.php
RewriteRule ^(.*)\.php$ $1 [R=301,L]
#Try to load php page if resource does not alreay have an extension
RewriteCond %{REQUEST_URI} !\.[a-z]+$
RewriteRule ^(.*)$ $1.php [L]
I have made a website in with Magento in one language 2 years ago and now I would like to add another language.
My actual structure is like that
example.com/category/
example.com/about.html
example.com/product.html
and I would like to obtain a structure like this:
example.com/lang1/category/
example.com/lang1/about.html
example.com/lang1/product.html
example.com/lang2/category/
example.com/lang2/about.html
example.com/lang2/product.html
This is not a big deal since Magento allowes me us to do that simply logging in the backend ->System -> Configuration -> Web -> Add store code to Urls (YES)
I can't do this setting right now because before that I need to fix and do rewrites from the first structure to the new one.
example.com/everything_without_the_/lang1/_path to a permanent
example.com/lang1/everything
I need for example that the urls from the backlinks from posts from the socials and other webistes won't get a 404 error but will automatically be redirected with a 301 redirection to the equivalent page in the new structure.
So I would like to add a script like this in the natural language:
rewrite permanently all the urls which ends not with /lang1/ or /lang2/ to urls with the prefix /lang1/
I know that I could manually add rewrite rules with this schema in the Magento backend but I would prefer to know if it is possible to batch this directly from the database or with some script in the .htaccess or index.php and above all which one between these solutions will have a less negative impact in SEO and SERP.
I've found a solution
Hi I've found a solution:
RewriteCond %{HTTP_HOST} ^(.)?example.com$ [NC]
RewriteCond %{REQUEST_URI} !^/it/
RewriteCond %{REQUEST_URI} !^/en/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /it/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.)?example.com$ [NC]
RewriteRule ^(/)?$ it/index.php [L]
at a first look works, but I not guarantee.
I hope this will help
Cheers
Try this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/lang1/
RewriteCond %{REQUEST_URI} !(.*)/lang2/
RewriteRule ^(.*)$ http://www.domain.com/$1/lang1/ [R=301,L]
you can use .htaccess 301 url to make if possible with your own regex function
As you need some specific requirement to do with your language part
here i am giving you category example
Redirecting Subcategory URL with New Parent URL
Examples:
category/sub1 redirect to cat/sub1
category/sub2 redirect to cat/sub2
category/sub3 redirect to cat/sub3
This may be needed if you changed a parent Category URL or even if you want to remove the parent URL completely:
RewriteRule ^category/(.*) http://www.yourwebsite.com/cat/$1 [R=301, L]
OR to remove '/category/' completely from yourwebsite.com/category/sub and end up with yourwebsite.com/sub ('sub' can be any url):
RewriteRule ^category/(.*) http://www.yourwebsite.com/$1/ [R=301, L]
And also you can refer
http://www.learnmagento.org/magento-tutorials/301-redirects-in-magento/
http://blog.maximusbusiness.com/2012/10/magento-url-rewriting-regex-and-301-redirects-tips/
hope this will sure help you.
I've found a solution
Hi I've found a solution:
RewriteCond %{HTTP_HOST} ^(.)?example.com$ [NC]
RewriteCond %{REQUEST_URI} !^/it/
RewriteCond %{REQUEST_URI} !^/en/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /it/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.)?example.com$ [NC]
RewriteRule ^(/)?$ it/index.php [L]
at a first look works, but I not guarantee. I hope this will help Cheers
I am totally new to attempting redirects in htaccess but have a list of about 20 urls, all with query strings and all going to different urls.
I have managed to get a redirect using a query string working using RewriteCond and RewriteRule but when I add the other urls in the same format they all seem to redirect to the url in the first RewriteRule.
Its getting so frustrated as I have searched everywhere and tried so many ways to try and get this working. Hopefully someone on here can help me!
Here are a couple of the urls I need to redirect:
/store/index.php?search=flip flops >> http://www.stonemenswear.co.uk/menswear/flip-flops
/store/index.php?search=Boss+Orange+Shorts >> http://www.stonemenswear.co.uk/menswear/shorts
And here is the code I have got so far:
RewriteEngine on
RewriteCond "%{QUERY_STRING} search=flip flops"
RewriteRule ^index\.php$ http://www.stonemenswear.co.uk/menswear/flip-flops/? [R=301,N]
RewriteCond %{QUERY_STRING} search=Boss+Orange+Shorts
RewriteRule ^index\.php$ http://www.stonemenswear.co.uk/menswear/shorts? [R=301,N]
(plus the rest of the rewrites in the same format)
Each of these are getting redirected to the flip flops page!
Thanks in advance.
Wrong use of flag N you need L flag instead. Replace your code with this:
RewriteCond "%{QUERY_STRING} search=flip flops" [NC]
RewriteRule ^store/index\.php$ http://www.stonemenswear.co.uk//menswear/flip-flops/? [R=301,L,NC]
RewriteCond %{QUERY_STRING} ^search=Boss\+Orange\+Shorts(&|$) [NC]
RewriteRule ^store/index\.php$ http://www.stonemenswear.co.uk/menswear/shorts/? [R=301,L,NC]
You have some small syntax errors here.
RewriteCond %{QUERY_STRING} "^search=flip flops$" [NC]
RewriteRule ^store/index\.php$ http://www.stonemenswear.co.uk/menswear/flip-flops/? [R=301,L]
RewriteCond %{QUERY_STRING} ^search=Boss\+Orange\+Shorts$ [NC]
RewriteRule ^store/index\.php$ http://www.stonemenswear.co.uk/menswear/shorts/? [R=301,L]
Using the following htaccess, I have been able to rewrite example.com/profile.php?username=xyz to example.com/xyz,
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?user=$1
Adding the following to the above,
RewriteRule ^([a-zA-Z0-9_-]+)/section$ section.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/section/$ section.php?user=$1
did not resolve example.com/section.php?username=xyz to example.com/xyz/section.
Am I doing something wrong here?
First of all: The manner of speaking would rather be the opposite: The rules you showed are to rewrite requests like /xyz internally to /profile.php?username=xyz and not vice versa.
Now if you want to rewrite requests like /xyz/section internally to /section.php?username=xyz where section and xyz are variable, try these rules:
RewriteRule ^([a-zA-Z0-9_-]+)/?$ profile.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/([^/]+)/?$ $2.php?user=$1
To look for static files (images, css) in the right directory without having to write the file address, do:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Before writing the code suggested by RageD
(Sorry, should have posted it as a comment but I needed newlines)
I tested it and it works. Try this:
RewriteRule ^([a-zA-Z0-9_-]+)\/section$ section.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)\/section\/$ section.php?user=$1
All I did was escaping the / in the URL since / is a regex delimiter.