rewrite url /blog/article.php?id=hello to /blog/hello.html - .htaccess

Im having some troubles with the redirection rules on my .htaccess file.
I would like to create a rule to redirect my blog content to a friendly url.
The current url structure is:
/blog/article.php?id=hello
and I would like it to change to:
/blog/hello.html
This are my rules so far, and I dont seem to be able to find the error:
RewriteEngine On
Options -MultiViews
RewriteRule ^blog/([a-z,A-Z,0-9]+)$.html blog/article.php?id=$1 [L]
Any help would be appreciated.

Because of your placement of $ in the pattern, the rewrite module is unable to match your request to the expression.
It should be:
Options -MultiViews
RewriteEngine On
RewriteRule ^blog/([\da-z]+)\.html$ blog/article.php?id=$1 [L,NC]

Related

htacces rewrite single file

I want to rewrite a specific file on my website to another one by using htaccess:
# General
RewriteEngine On
RewriteBase /
Options All -Indexes
Options +FollowSymLinks
# Rewrite file
RewriteRule ^/file.html$ /dir/file.html [L]
This is the .htaccess code i'm using based on snippets i found on the internet.
Somehow this is not working, the server is returning a 404-Not-found error.
I can't see any difference with example's that are said to work, like in
Rewriting path for a specific file using htaccess
Edit:
If I place file.html in the root-folder, I can view it. So the rewrite definitely is not happening.
RewriteRule does not receive leading slash. Write so:
RewriteRule ^file.html$ /dir/file.html [L]

Redirect URL by query string match?

I'm trying to do a 301 redirect using .htaccess file to redirect:
http://www.mydomain.com/index.php?/knowledgebase/
To:
http://www.mydomain.com/knowledgebase/
Ive tried about 30 different redirect/rewrite suggestions but none work, can anyone provide the specific .htaccess setting I need please?
You can do this with mod_rewrite like this:
RewriteCond %{QUERY_STRING} ^/knowledgebase/$
RewriteRule ^index.php$ /knowledgebase/ [L,R=301]
Edit: When debuging htaccess rewrite rules I find this tool very useful

Change URL but stay on the same page using htaccess

I have a URL:
www.example.com/property-listings/united-states/colorado/denver/denver-co-office-space
I want to stay on the same page above but simply display the URL in the address bar like this:
www.example.com/property-listings/united-states/colorado/denver/office-space
How do I accomplish this using htaccess and rewrite rules?
If I understood right, try writing a rule like this one:
RewriteEngine on
RewriteRule property-listings/united-states/colorado/denver/office-space http://www.example.com/property-listings/united-states/colorado/denver/denver-co-office-space [L]
OK. You didn't supply a pattern or mentioned there was any, so I have to guess the pattern is up to /denver/ subdirectory. Try this:
RewriteEngine on
RewriteRule ^(property-listings/united-states/colorado/denver/)(office-space)/?$ $1denver-co-$2 [L]
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 /
RewriteRule ^(property-listings/united-states/colorado)/(denver)/(office-space)/?$ $1/$2/$2-co-$3 [L,NC]

Rewrite subfolder to index.php file

i want to rewrite the url http://www.host.com/de/rss to the file http://www.host.com/index.php?type=9000001&L=1
My rewrite rule look like this:
RewriteRule ^de/rss(.*) index.php?type=9000001&L=1
But this does not work. If i delete de/ it works with the corresponding url.
I tried to look for similar questions here on stackoverflow, but that didnĀ“t help.
Use this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^de/rss/?$ index.php?type=9000001&L=1 [L,QSA,NC]
And if doesn't work then please post your full .htaccess in your question.
Maybe you should try this:
RewriteRule ^/de/rss.*$ /index.php?type=9000001&L=1

Possible to change links and redirect with htaccess?

This is my .htaccess
RewriteEngine on
Options +FollowSymlinks -MultiViews
RewriteRule ^genre/(.*)$ /index.php?genre=$1 [R=301]
The mod_rewrite works, but so does the old link example:
/index.php?genre=action
Still works. I want to redirect it to:
/genre/action
Problem: They both work, I want the old one to redirect to the new one.
And is it possible to change links with .htaccess. Some sort of url replacing?
My links still says:
www.site.com/index.php?genre=action
I want it to change it to:
www.site.com/genre/action
Or do I have to do this manually?
Thanks!
[R=301] leads to a HTTP redirect with status code 301.
Just write
RewriteEngine on
Options +FollowSymlinks -MultiViews
RewriteRule ^genre/(.*)$ /index.php?genre=$1
to rewrite the urls on the server side.

Resources