I am using below code on my .htaccess file
RewriteRule ^([^/]*)/([^/]*)$ /view_basket.php?order_id=$1&pin=$2 [L]
the goal is to redirect a clean URL like below
http://www.zire20.ir/77438/9512
to this one
http://www.zire20.ir/view_basket.php?order_id=77438&pin=9512
The thing is it was working on my previous server but now I changed to godaddy hosting and it's not working! any idea ?
p.s:
and my whole .htaccess file is like below:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^zire20.ir [NC]
RewriteRule ^(.*)$ http://www.zire20.ir/$1 [L,R=301]
RewriteRule ^([^/]*)/([^/]*)$ /view_basket.php?order_id=$1&pin=$2 [L]
RewriteRule ^([^/]*)/([^/]*)$ /view_basket.php?order_id=$1&pin=$2 [L]
lots of photos are not loading!
The problem with your current rule is that you are rewriting unconditionally. Any URL that contains a single slash will get rewritten. I imagine that some of your (static) photo URLs match this pattern.
Common practise is to only rewrite the URL if it doesn't match an existing file (or directory):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ /view_basket.php?order_id=$1&pin=$2 [L]
This makes sure the request is only rewritten for non-existing files (not a file or a directory). I've also made the pattern a little more restrictive so there must be 1 or more chars before and after the slash (+), instead of 0 or more (*).
The thing is it was working on my previous server
I can't see how this was possible, unless the URL structure was different on the previous server?
Related
I have this piece of line in my htaccess that causes errors. I couldn't find a similar answer to my inadequate wording.
I am attempting to get "username/followers, username/following, etc" and also "settings/account, settings/password, etc". I stopped using sub folders for non-scripts and images, so everything is on the same level.
Now I know they have similar casing, but I am curious how Facebook, Twitter, etc manage to do this.
Do they condense to one large page to make it work? I know they prevent people from using settings and other root level names from being used, and I haven't quite gotten to that point myself.
RewriteRule ^([^/]+)$ profile_home.php?userdomain=$1 [L]
RewriteRule ^([^/]+)/([^/]+)$ profile_home.php?userdomain=$1&selection=$2 [L]
RewriteRule ^settings/$ profile_settings.php [L]
RewriteRule ^settings/([^/]+)$ profile_settings.php?selection=$1 [L]
RewriteRule ^settings/([^/]+)/([^/]+)$ profile_settings.php?selection=$1&upload=$2 [L]
If I remove
RewriteRule ^([^/]+)/([^/]+)$ profile_home.php?userdomain=$1&selection=$2 [L]
Then everything works fine. How do I make this work with two pages?
I could do
RewriteRule ^settings/([^/]+)/$ profile_settings.php?selection=$1 [L]
But it doesn't look as nice. If not right place to ask, please let me know.
Your second rule matches both directory structures. You can use a negative lookahead so requests starting with the setting are not matched by that rule.
^(?!settings)([^/]+)/([^/]+)$
You can read more about lookaheads here:
http://www.rexegg.com/regex-lookarounds.html
http://www.regular-expressions.info/lookaround.html
Here is my solution
RewriteEngine On
# make sure to add your document root dir
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([\w-.]+)/?$ index.php?id=$1 [L,QSA]
RewriteCond %{DOCUMENT_ROOT}/htdocs/user/$2 -f
RewriteRule ^([\w-]+)/(.+)/?$ $2?id=$1&goto=$2 [L,QSA]
RewriteCond %{DOCUMENT_ROOT}/htdocs/user/$2/index.php -f
RewriteRule ^([\w-.]+)/([a-z0-9]+)/?$ $2/index.php?id=$1&goto=$2 [NC,L,QSA]
RewriteRule ^([\w-.]+)/([a-z0-9]+)/?$ index.php?id=$1&goto=$2 [NC,L,QSA]
so inside your server root dir htdocs/user/ you must have a folder like /user/ and a file index.php this htacess will replace the file site/user/index.php to site/user/username in the same user dir you need to have the following.php file so site/user/username/following.php in the same folder, I think you understand my answer.
I need to write optimized .htaccess rules. I had written like that and they are working
RewriteEngine on
RewriteRule ^city1-name-in-url/$ products.php?city=city1-name-in-url
RewriteRule ^city2-name-in-url/$ products.php?city=city2-name-in-url
RewriteRule ^city3-name-in-url/$ products.php?city=city3-name-in-url
RewriteRule ^city4-name-in-url/$ products.php?city=city4-name-in-url
And url for these rules are http://www.example.com/global/city1-name-in-url/
I have to write these rules for 800 cities which is making website slow. I want to know if there is anyway to get the part url and use it in RewriteRule
Example like
If url : http://www.example.com/global/any-city-name/
RewriteRule ^any-city-name/$ products.php?city=any-city-name
This is possible? To get the part of url after global and then use it in rewrite rule.
You can use this rule in /global/.htaccess:
RewriteEngine On
# 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
RewriteRule ^([\w-]+)/?$ products.php?city=$1 [L,QSA]
[\w-]+ matches 1 or more of [a-zA-Z0-9_-] characters.
After banging my head against this for the better part of a week, it turned out to be the same problem, and solution, as in this thread: RewriteCond in .htaccess with negated regex condition doesn't work?
TL;DR: I had deleted my 404 document at some point. This was causing Apache to run through the rules again when it tried to serve the new page and couldn't. On the second trip through, it would always match my special conditions.
I'm having endless trouble with this regex, and I don't know whether it's because I'm missing something about RewriteCond or what.
Simply, I want to match only top-level requests, meaning any request with no subdirectory. For example I want to match site.com/index.html, but not site.com/subdirectory/index.html.
I thought I would be able to accomplish it with this:
RewriteCond %{REQUEST_URI} !/[^/]+/.*
The interesting thing is, it doesn't work but the reverse does. For example:
RewriteCond %{REQUEST_URI} /[^/]+/.*
That will detect when there is a subdirectory. And it will omit top-level requests (site.com/toplevelurl). But when I put the exclamation point in front to reverse the rule (which RewriteCond is supposed to allow), it stops matching anything.
I've tried many different flavors of regex and different patterns that should work, but none seem to. Any help would be appreciated. this Stack Overflow answer seems like it should answer it but does not work for me.
I've also tested it with this .htaccess rule tester, and my patterns work in the tester, they just don't work on the actual server.
Edit: by request, here is my .htaccess. It allows URLs without file extensions and also does something similar to a custom 404 page (although its purpose is to allow filenames as arguments, not be a 404 replacement).
Options +FollowSymLinks
DirectoryIndex index.php index.html index.htm
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME} =/home/me/public_html/site/
RewriteRule ^(.*)$ index.php
RewriteCond %{REQUEST_FILENAME} !-f # Below this is where I would like the new rule
RewriteRule ^(.*)$ newurl.php
</IfModule>
I want to match site.com/index.html, but not site.com/subdirectory/index.html
You can use:
RewriteRule ^[^/]+/?$
Or using RewriteCond:
RewriteCond %{REQUEST_URI} ^/[^/]+/?$
Hi people at Stackoverflow,
I've honestly searched here and tried stuff but since I'm really not an expert and nothing worked, I'm lost and need your help. I don't post here often because usually I find what I am looking for.
The situation
I have different sites running on the same server. Let's say that one of my websites is called Cats. It runs with CMSMS and resides in a subfolder of my root called cats.com.
In the root of my server there is a htaccess file with this code (generated by the server admin):
RewriteCond %{HTTP_HOST} ^www.cats.com$
RewriteCond %{REQUEST_URI} !^/cats.com/
RewriteRule (.*) /cats.com/$1
RewriteCond %{HTTP_HOST} ^cats.com$
RewriteCond %{REQUEST_URI} !^/cats.com/
RewriteRule (.*) /cats.com/$1
In the cats.com subfolder there is a htaccess file with rewrite code for pretty urls:
# RewriteBase /cats.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
This works. When I request www.cats.com I see the contents of the /cats.com subfolder in my root but the URL base stays www.cats.com and all the URLS are extremely pretty.
However, due to a setup error (my bad) in my site's config file, the URL listed in Google is
http://www.root.com/cats.com
When I click the URL, I see the correct page but the address bar reads http://www.root.com/cats.com. The pretty URL rewrite works, but I want this url to rewrite to www.cats.com.
What I have tried
I have tried rewriting the Google listed URL with
attempt one
I tried this in both htaccess files, below and above the existing rewrite rules.
RewriteRule ^/cats.com(.*)$ http://www.cats.com [R=301,L]
RewriteRule ^/cats.com/(.*)$ http://www.cats.com/$1 [R=301,L]
Nothing happens. Everything stays the same.
attempt two
Again tried this in both htaccess files, below and above existing rewrite rules
Redirect 301 /cats.com http://cats.com
This results in an infinite loop in all occasions.
I hope my description is clear enough...
If anybody has any idea what might / should work I'd love to hear it.
Thank you for your help!
Change your rules in cats.com subfolder's htaccess file to this:
RewriteEngine On
RewriteBase /cats.com/
RewriteCond %{THE_REQUEST} /+(cats\.com)/(\S+) [NC]
RewriteRule ^ http://%1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA,L]
I have a tricky issue redirecting some URLs internally for my site.
The situation is this, I currently have a URL like example.com/check/youtube.com which internally redirects to check.php?domain=youtube.com using the following mod_rewrite code:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]
RewriteRule ^offline offline.php [NC,L]
RewriteRule ^error error.php [NC,L]
RewriteRule ^check/(.*)$ check.php?domain=$1 [NC,L]
However I would also like to be able to redirect to check.php using a URL like example.com/youtube.com. Unfortunately it is just beyond me to figure it out.
I have a directory /assets/ with all the CSS, JS, etc. which shouldn't be affected.
Thanks
Try this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^/.]+\.[^/]+$ check.php?domain=$0 [L]
This rule rewrites any request with a URL path of the form [^/.]+\.[^/]+ (a string that contains at least one dot but no slashes at all) that cannot be mapped to an existing file to your check.php.
As you want to redirect "example.com/youtube.com" does that mean you wish to redirect pretty much anything? What is specifically allowed to be passed, e.g. would I be allowed to pass "example.com/youtube.com/foobar.php" for a redirect to check.php?