I'm trying to get a url to rewrite using htaccess but can't seem to get it working.
I'm trying to rewrite http://website.com/pages/blog/article.php?article=blog-entry so that it can be entered as http://website.com/pages/blog/blog-entry but i'm getting an error when I try the following:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^pages/blog/(.+)$ pages/blog/article.php?article=$1 [NC,L]
Can anybody see where i'm going wrong as this just gives me a 404 error. Thanks in advance.
Use this rule inside /pages/blog/.htaccess:
RewriteEngine on
RewriteBase /pages/blog/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ article.php?article=$1 [QSA,L]
I'm trying to rewrite
http://website.com/pages/blog.php?article=blog-entry so that it can be
entered as http://website.com/pages/blog/blog-entry but i'm getting an
error when I try the following:
RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond
%{REQUEST_FILENAME}\.php -f
RewriteRule ^pages/blog/(.+)$ pages/blog/article.php?article=$1
[NC,L]
Your wording is confusing, but I believe this is what you mean:
The real url is: http://website.com/pages/blog.php?article=blog-entry
you want to be able to use a 'friendly' url: http://website.com/pages/blog/blog-entry to point to the real url.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^pages/blog/(.+)$ /pages/blog/article.php?article=$1 [QSA,L]
The first two tests ask: is this a directory that exists? is this a file that exists? Because article.php is a file, it won't be included in this action, so you won't enter into an endless loop, which is always the risk with incorrectly done rewrite rules.
Take the given url, and use query string append (QSA) to attach the desired data to the actual file that will process the request. This is not a rewrite in that the url the user sees does not change, this only happens internally in apache, which sends the request to the desired target, with the desired information.
You have to test if the file or directory exists because otherwise you'd be applying this rule incorrectly, since it should only be applied when the target does NOT exist. This is basically how all blog/cms 'search engine friendly urls' work, more or less.
Last, since the target is /blog.php?article=blog-entry you can't skip the leading /.
However, it's unclear to me why you'd want the friendly url to be so long, when you can just make it short, and friendlier: like, pages/[article-name]
Related
OK so I've found every rendition of this but not this specifically. So my urls are in this format
www.inspection.com/users/?action=register
I want to convert it to
www.inspection.com/users.php?action=register
Here's what I've got so far in my htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[A-Za-z0-9]+/\?action=[A-Za-z0-9]+$ $1.php?action=$2 [L,QSA]
But error logs say RewriteRule: cannot compile regular expression
Main Goal: After the domain name is the reference to the file I want to call. So append php on the end of that string but keep the query parameters
With your shown samples, attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs. Also make sure that your .htaccess rules file and .php file are residing on same folder.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/?]*)/?\?action=(.+)$ $1.php?action=$2 [NC,L,QSA]
One more thing, you have not created a capturing group on left side of RewriteRule hence you can't use them on the right side of it.
I am having trouble finding a solution for what I want to do. I am going to have a couple thounsand affiliates signing up on our site. They will provide a username, and basically give out the url www.domain.com/affiliate/username to their clients.
So, the url I will be starting with is www.domain.com/affiliate/home.html?userid=bob (if bob is the username they give).
I need that to check with the mysql database if that userid exists, then redirect it to the www.domain.com/affiliate/bob url.
Right now, I have this in my .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\/]+)$ affiliate/home.html?affiliate=$1 [L]
It obviously doesnt work and Ive never dealt with .htaccess before. Any help is greatly appreciated. Thank you.
Try this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^affiliate/([^/]+)/?$ /affiliate/home.html?affiliate=$1 [L]
As for redirecting to the nicer looking URL if someone enters the one with the query string:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /affiliate/home.html\?affiliate=([^\ &]+)&?([^\ ]*)
RewriteRule ^ /affiliate/%2?%3 [L,R=301]
There's no way to check if a username is in your database before the rewrite happens, using an htaccess file. There's ways to talk to a database in apache 2.4 or using a RewriteMap (which can only be defined in server or vhost config, not in htaccess) but you're better off doing the check in your home.html and just return a 404 if the user isn't found.
See my answer on an other similar question.
You want www.domain.com/affiliate/home.html?userid=bob to be redirected to /affliate/bob. You want /affliate/bob to be rewritten to /affiliate/home.html?userid=bob.
Add this to your .htaccess in your /www/ or /public_html/.
RewriteEngine on
RewriteCond %{QUERY_STRING} ^affiliate=([^&]*)$
RewriteRule ^affiliate/home\.html /affiliate/%1? [R,L]
RewriteRule ^affiliate/(.*)$ affiliate/home.html?affiliate=$1&r=0 [L]
The first rule matches if the user tries to access affiliate/home.html?userid=bob. The RewriteCondition matches the part of the query string (after the ?) that you need to write the fancy url. %1 matches the first 'capturing group' in a rewrite condition. The ? at the end will clear the query string. [R] means it is a redirect (see the linked answer what that does). [L] means it will stop matching if this one matches.
The second rule is an internal rewrite. $1 matches the first 'capturing group' in the RewriteRule. The trailing &r=0 is there to stop the first rule from matching. It requires a RewriteCond %{REQUEST_FILENAME} !-f to function properly, because the fancy url is in the same directory is the file that handles it.
I've got a an old website that I've taken over. New users essentially get a custom page created for them. It was done in a less than fantastic manner. Currently it actually generates a file named for the slug URL created and symbolically links it to a folder called "/main/". Obviously I want to change this. My plan was simply to have it redirect non-existant folders to "/main/" via .htaccess. Currently this is what my .htaccess looks like:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt|index\.htm)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /main/ [L]
However this generates a redirect loop. What am I missing?
Edit
On that note, I realized I should say I want it to maintain the path that's input. For example if I put http://www.mydomain.com/test_person it should maintain that address, but forward everything to the main folder if that makes sense.
You'll want your rule to not rewrite URLs already beginning with main in order to not have it loop, eg:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !^main/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^/]+/(.*)$ /main/$1 [L]
[L] doesn't really mean what you would think [L] should mean in the context of a .htaccess file.
You should be able to get the original url requested from the REQUEST_URI environment variable, but a common thing to do is to pass the slug to the target as a GET variable.
RewriteRule ^([^/]+)/(.*)$ /main/$2?user=$1 [QSA,L]
Or pass everything to a single script which interprets the URL and finds the correct content to return:
RewriteRule ^([^/]+)/(.*)$ /main/index.php?user=$1&path=$2 [QSA,L]
I have following rewrite rules for a website:
RewriteEngine On
# Stop reading config files
RewriteCond %{REQUEST_FILENAME} .*/web.config$ [NC,OR]
RewriteCond %{REQUEST_FILENAME} .*/\.htaccess$ [NC]
RewriteRule ^(.+)$ - [F]
# Rewrite to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !^(/bilder_losning/|/bilder/|/gfx/|/js/|/css/|/doc/).*
RewriteRule ^(.+)$ index.cfm?smartLinkKey=%{REQUEST_URI} [L]
Now I have to exclude a script including its eventually querystrings from the above rules, so that I can access and execute it on the normal way, at the moment the whole url is being ignored and forwarded to the index page.
I need to have access to the script shoplink.cfm in the root which takes variables tduid and url (shoplink.cfm?tduid=1&url=)
I have tried to resolve it using this:
# maybe?:
RewriteRule !(^/shoplink.cfm [QSA]
but to be honest, I have not much of a clue of urlrewriting and have no idea what I am supposed to write. I just know that above will generate a nice 500 error.
I have been looking around a lot on stackoverflow and other websites on the same subject, but all I see is people trying to exclude directories, not files. In the worst case I could add the script to a seperate directory and exclude the directory from the rewriterules, but rather not since the script should really remain in the root.
Just also tried:
RewriteRule ^/shoplink.cfm$ $0 [L]
but that didn't do anything either.
Anyone who can help me out on this subject?
Thanks in advance.
Steven Esser
ColdFusion programmer
Please try to put the following line at the top of your config (after RewriteEngine on):
RewriteRule ^shoplink.cfm$ - [L]
I’m trying to use the following .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^images/
RewriteRule (.*) view.php?picid=$1 [L]
RewriteRule ^/user/(.*)$ /users.php?user=$1
I want two things to happen: Whenever someone requests /1234, it redirects to /view.php?picid=1234, and also when someone visits /users/bob, it redirects to /users.php?user=bob.
My code however, doesn’t seem to be working correctly.
There are several ways to do that. Here’s one that should work:
RewriteRule ^user/(.+)$ users.php?user=$1 [L]
RewriteRule ^([0-9]+)$ view.php?picid=$1 [L]
The first rule will catch any request that’s URI path begins with /user/ followed by one or more arbitrary characters. And the second will catch any request that’s URI path begins with / followed by one or more digits.
The initial problem with your rules is that the RewriteRule with (.*) will match everything.
If you do not want it to match a URL with a slash in it (such as users/bob), try ^([^/]*)$
Secondly, after a URL is rewritten, the new URL goes through your rules again. If you want to avoid matching something that has already been rewritten once, you should add a condition like
RewriteCond %{REQUEST_URI} !\.php