I want to rewrite all URLs to a certain file (/blog/post.php) within certain folders (/blog) except a couple of files (/blog/post.php of course and the root of the directory /blog/index.php or /blog/). I therefore have this:
RewriteRule ^blog/index.php - [L]
RewriteRule ^blog/post.php - [L]
RewriteRule ^blog/(.*) /blog/post.php [L,QSA]
Where the intended result is that all URLs except index.php and post.php get rewritten to post.php
The problem is if I put this in the .htaccess in the site root, when you type just website.com/blog/ it rewrites to page to post.php whereas website.com/blog/index.php doesn't.
If I put the same code in the .htaccess file in the blog folder (but without the word blog in each line) it works as expected/wanted.
I'm very sorry for another RewriteRule question but its a specific example/problem I've not been able to find and I would be very grateful if anyone had any tips.
Have your single rule like this:
RewriteCond %{REQUEST_URI} !/blog/(index|post)\.php$ [NC]
RewriteRule ^blog/(.+)$ /blog/post.php [L]
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 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?
I have a file at www.domain.com/blog/blog.html that I want to rewrite as www.domain.com/blog.
I have figured out the rewrite for removing the extension .html but am having either circular issues or end up pointing the blog rewrite to the blog folder when trying to do the above.
I am new to rewrite syntax, so help would be much appreciated!
Create /blog/.htaccess if it doesn't already exist and place this rule:
RewriteEngine On
RewriteBase /blog/
RewriteRule ^/?$ blog.html [L]
# To internally forward /blog/file to /blog/file.html
RewriteCond %{DOCUMENT_ROOT}/blog/$1\.html -f [NC]
RewriteRule ^(.+?)/?$ $1.html [L]
So I have a legacy site archived with 1000s of image URLs like
img/2265/thumb/Incubator.jpg?1344891444
and the file it points at is sitting at:
img/2265/thumb/Incubator-1344891444.jpg
some of the files are jpegs, pngs etc, but they all follow this pattern of a hyphen then querystring value then extension. (keeping rest of path the same)
So far I have got the Querystring part but cannot figure out how to combine this with a RewriteRule (?) to keep the filename but add in the matched value.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^([0-9]*)$
RewriteRule PATTERN_TO_MATCHJPG|PNG|GIF$ SOMETHING-$1.jpeg [L]
Is it that the RewriteRule first part should be a rewritecond? :-/
thanks for any tips.
UPDATED to show answer in nice format!**
RewriteCond %{QUERY_STRING} ^([0-9]+)$
RewriteRule (.+?)\.(png|gif|jpg|jpeg|JPG|JPEG|PNG|GIF)$ $1-%1.$2? [L]
You can use this rule:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^([0-9]+)$
RewriteRule ^(.+?)\.(png|gif|jpe?g)$ /$1-%1.$2? [L,NC]
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?