Rewrite URL with Query and directory in path - url

I have a URL of the form:
http://www.example.com/ads/story.asp?story_id=73
that I would like to replace with:
https://www.example.com/news/awards-2015/
I have tried several methods from other stackoverflow posts, including the following:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/ads/story.asp$
RewriteCond %{QUERY_STRING} ^story_id=73$
RewriteRule ^(.*)$ https://www.example.com/news/awards-2015/ [R=301,L]
... without success.
Can anyone suggest what I might be doing wrong? Key differences from other examples appear to be 1) rewriting from an http URL to an https URL and 2) the additional directory "news" in the path of the URL to be rewritten.
Any help would be appreciated!

Got it working.
RewriteEngine on
RewriteCond %{QUERY_STRING} (^|&)story_id=73(&|$) [NC]
RewriteRule ads/story_detail.asp(.*)$ https://www.example.com/news/awards-2015/? [R=301,L]

Related

How to redirect page by using .htaccess

I am using given below code to redirect to login page,When enter url in address bar e.g.(exmple.com/web/) then redirect to "http://exmple.com/web/login.php" How ?
RewriteEngine On
RewriteCond %{HTTP_HOST} ^exmple.com/web/$
RewriteRule ^$ http://exmple.com/web/login.php [L,R=301]
Try this
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.exmple\.com$
RewriteRule ^$ http://www.exmple.com/web/login.php[L,R=301]
%{HTTP_HOST} variable represents the current domain name without the path segment . Your RewriteCond is invalid because it is trying to match example.com/web/ as domain name which is a non existent domain and the RewriteCond fails.
Change the following condition :
RewriteCond %{HTTP_HOST} ^exmple.com/web$
to
RewriteCond %{HTTP_HOST} ^(www\.)?exmple.com$
You can use the following :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?exmple.com$
RewriteRule ^(web/?)?$ http://exmple.com/web/login.php [L,R=301]
You can also use RedirectMatch in your htaccess to redirect an url to a diffrent location :
RedirectMatch ^/web/?$ http://exmple.com/web/login.php
Clear your browser's cache before testing these solutions.

htacces redirection

I would like to remove the filename from a "subdirectory" URL and just display the subdirectory in the browser URL.
So:
www.domain.com.au/login/public_index.php
becomes:
www.domain.com.au/login/
I have tried various solutions from this site and the internet, but none have worked.
If possible I would like to put the new redirection in the root htaccess file?? and not in the subdirectory htaccess file?
* Just in case other redirections in the htaccess file are in conflict with any new subdirectory redirections, here are the other (and necessary) redirections in the htaccess file that perform important jobs:
#(1) A redirection to remove root level index.php from the url
# Redirect index.php to domain.com.au
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.domain.com.au/ [R=301,L]
#(2)
# Redirect domain.com.au to www.domain.com,au
RewriteCond %{HTTP_HOST} ^domain.com.au [NC]
RewriteRule ^(.*)$ http://domain.com.au/$1 [L,R=301]
#(3)
# Force SSL on login directory
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} login
RewriteRule ^(.*)$ https://www.domain.com.au/$1 [R,L]
Any help would be appreciated as all solutions to add the new subdirectory URL redirect thus far have failed.
Regards,
Peter
You can do something similar to what you did in #(1):
#(4)
# remove public_index.php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+)/public_index\.php\ HTTP/
RewriteRule public_index\.php$ http://www.domain.com.au/%1/ [R=301,L]
# internally add it back
RewriteCond %{REQUEST_URI} ^/([^/]+)/?$
RewriteCond %{DOCUMENT_ROOT}/%1/public_index.php -f
RewriteRule ^ /%1/public_index.php [L]
That's for any subdirectory, but if you're only interested in login, that gets easier:
#(4)
# remove public_index.php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /login/public_index\.php\ HTTP/
RewriteRule public_index\.php$ https://www.domain.com.au/login/ [R=301,L]
RewriteRule ^/?login/?$ /login/public_index.php [L]

url rewriting a query string

I want to mod-rewrite url and i am just wondering how do i rewrite my url of querystring type
http://localhost/folder/index.php?param1=value1&param2=value2&param3=value3
into
http://localhost/folder/value1/value2/value3
I have this htaccess file please tell me if it correct
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^index/(w+)/(w+)/(w+)$ /index.php?param1=$1&param2=$2&param3=$3 [nc]
Use this code in your DOCUMENT_ROOT's .htaccess file:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{QUERY_STRING} !param1= [NC]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)$ $1/index.php?param1=$2&param2=$3&param3=$4 [QSA,L]

.htaccess non-www to www URL Redirect Erases any URL path beyond base url

When visitors go, to say,:
http://website.com/dir/dir2/file.html
My re-write clears the path beyond the base url and redirects them to the home page with www. in the front:
http://www.website.com/
How can I prevent the stripping from occurring and sending visitors to the homepage instead of their requested url? FYI, the URLs in question DO exist.
Thanx!
Include the file/path info on the end of the rewrite like this:
RewriteCond %{HTTP_HOST} !^www\.website\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://www.website.com/$1 [L,R]
FYI, this is for a Magento site.
In .htaccess, after
#RewriteBase /magento/
After it I placed on their own lines:
RewriteCond %{HTTP_HOST} !^www\.website\.com$ [NC]
RewriteRule ^(.*)$ http://www.website.com/$1 [L,R=301]
And it works. Dad with kids also had it right, just don't know what the second line was for.
Most people need RewriteBase set, so try:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^(?:www\.your-live-domain\.com|staging\.domain\.com)$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule .* http://www.your-live-domain.com/$0 [L,R=301]
Use 301 to make it permanent

Redirect "mydomain.com/user" to "mydomain.com/name.php?id=user"

I think I can use the .htaccess file for this, but I've looked it up and not found anything useful. What I want to do is have my site redirect to a php page when a user types their username in the URL like:
example.com/username
And have it be the same as a PHP page like:
example.com/name.php?id=username
I'd like it to display as example.com/username even after it redirects, but it is not necessary. Any ideas?
You can use mod_rewrite to transparently rewrite your URLs on the server.
Assuming that you'd only have usernames following your domain, something like this would do what you want:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .+ name.php?id=$0
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+) users.php?user=$1 [L]
I think that will work.
The Apache mod_rewrite guide is here
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
You probably want Apache's mod_rewrite.

Resources