How to rewrite part of URL in .htaccess? - .htaccess

I have this URL:
example.com/news/article/254
How to make a PHP script to process this URL (example.com/blog/article/254) as example.com/news/article/254,
and example.com/blog as example.com/news, etc.

The following code should work if you add it to your .htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^blog/article/(.+?)\/?$ /news/article/$1 [R=301, L]
</IfModule>
If you don't want to tell robots etc. that you have permanently redirected then take out the "R=301, " above.

Related

htaccess simple redirect doesnt work

Hi I need to redirect using htaccess every request which points at:
http://www.mydomain.com/index.php?option=com_content&view=category&layout=blog&id=293&Itemid=387
to this url:
http://www.otherdomain.com
I've try to do it by:
redirect /index.php?option=com_content&view=category&layout=blog&id=293&Itemid=387 http://www.otherdomain.com
But it doesnt work. So I need Your help.
Better to use mod_rewrite for this stuff.
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 /
RewriteCond %{QUERY_STRING} ^option=com_content&view=category&layout=blog&id=293&Itemid=387$
RewriteRule ^index\.php$ /? [L,R=302,NC]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.
I think someone may need to extend my answer but you'll be looking to do something along the lines of:-
RewriteRule ^([^/]+)/? index.php?option=$1 [R=301,L]
The rule will require a regular expression so the server can compare the request.

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]

.htaccess mod-rewrite -- Not Redirecting

Okay, so I have a script that works like adf.ly; you submit a URL, the Url is shortened and then an interstitial advertisement shown before you're taken to your URL. I have the following .htaccess located in the root:
DirectoryIndex index.php
FileETag none
ServerSignature Off
Options All -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([0-9a-zA-Z]{1,6})$ fly/?to=$1 [L]
RewriteRule ^([0-9]{1,9})/banner/(.*)$ fly/?uid=$1&adt=2&url=$2 [L]
RewriteRule ^([0-9]{1,9})/(.*)$ fly/?uid=$1&adt=1&url=$2 [L]
</IfModule>
The script is generating the shortURL's (you can try here: http://www.twitsym.com/short/) however it is not redirecting to fly.php and then the final URL. I'm terrible with .htaccess and have little to no knowledge. Can anybody edge me further as to what might be causing the problem?
Directory structure is:
.../
.../fly/index.php
Thank you again, StackOverflow!
it seems that you will need the LogLevel directive somewhere in your httpd.conf or vhost.conf if this is a virtual server. See this post for a similar question.
The code would be:
RewriteLogLevel 3
RewriteLog "/usr/local/var/apache/logs/rewrite.log"
Where is the htaccess supposed to be redirecting to (i.e. which URL should lead whereto?)
Additionally, you asked for being redirected to fly.php, however it does not occur in your .htaccess. Was this your intention?

htaccess rewrite rule similar to WordPress

Ok I have a script in the folder 'intake'. The index.php file auto creates a list of urls. Those urls are in the format:
/intake/4/Westrop
I have an .htaccess file in the intake folder. I want it to redirect the url to a FILE.
The above example would then become /intake/redirect.php?id=4&name=Westrop
Here's what I have so far:
DirectoryIndex index.php
Options All -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)/(.*)$ /intake/redirect.php?id=$1&name=$2 [L]
</IfModule>
But for some reason if I type /intake or /intake/index.php I get a 404 error. According to firebug its trying to take "intake" and turn it into "intake.php"
Any ideas?
Place the following code in .htaccess file in website root folder:
RewriteEngine On
RewriteRule ^intake/(\d+)/([a-z0-9\-_]+)$ /intake/redirect.php?id=$1&name=$2 [NC,QSA,L]
P.S.
Do not use this sort of pattern -- ^(.*)/(.*)$ -- in your case for longer URLs it will work not as you would expect. It will match your URL .. but in a wrong way.
Start your .htaccess with this option:
Options +All -MultiViews -Indexes

How to redirect a single web page from one domain to another using .htaccess

How do I get the following redirect to work?
olddomain.com/employee-scheduling-software.html
To redirect to
newdomain.us/employee-scheduling-software.html
I do have mod_rewrite on, but I'm basically a complete novice in this area
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^employee-scheduling-software.html$ http://newdomain.example.org/employee-scheduling-software.html
</IfModule>
You can change the rule into:
RewriteRule ^employee-scheduling-software.html$ http://newdomain.example.org/employee-scheduling-software.html [R=301]
which will send a 301 Moved Permanently header to the browser, so it updates its bookmarks and stuff.
You could use this code in .htaccess on olddomain.com:
RewriteEngine on
RewriteRule ^employee-scheduling-software\.html$ http://newdomain.us/employee-scheduling-software.html [R,QSA]
Since the ^employee-scheduling-software\.html$ is a PERL regex, you need to escape the dot in ".html" with a backslash (\.).
This will just redirect employee-scheduling-software.html to the new domain. If you want to redirect all files to the new domain, use:
RewriteEngine on
RewriteRule ^(.*)$ http://newdomain.us/$1 [R,QSA]

Resources