.htaccess needs to check for blank parameter - .htaccess

Currently, my .htaccess located at olddomain.com contains:
RewriteEngine On
RewriteRule ^([^/]+)/? http://newdomain.com/?d=$1 [L]
So if one goes to:
olddomain.com/docu443
It's redirected to:
newdomain.com/?d=docu443
This is working fine except where there is a blank parameter:
olddomain.com
is redirected to:
newdomain.com/?d=default.asp
Instead, I need it to go to:
newdomain.com
with no parameters.
Yes, I'm a complete Apache noob so please spell out your answer please because otherwise I won't understand what you are talking about. Thank you in advance!

You can try adding a condition:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/? http://newdomain.com/?d=$1 [L]
Or check for the exception:
RewriteEngine On
RewriteRule ^default.asp$ http://newdomain.com/ [L]
RewriteRule ^([^/]+)/? http://newdomain.com/?d=$1 [L]

Related

Htaccess Redirect issues in joomla

Please help me with a redirect issue.
I am trying to redirect from http://www.project/index.php/blog to http://www.project/blog.
I tried the following but it did not work
RewriteRule ^(blog)$ ./index.php/ [L]
Give this a try:
Options +FollowSymlinks
RewriteEngine on
Rewriterule ^index.php/blog(.*)$ http://www.project/blog$1 [r=301,nc]
If you want to append the requested path info at index.php, you might use this RewriteRule
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteRule ^ /index.php%{REQUEST_URI} [L]

Htaccess - Rewrite engine (reverse engineering a line of code)

On a site I'm working on, if you enter the url, plus 1 directory, the htaccess adds a trailing slash.
So, this: http://www.mysite.com/shirts
Becomes this: http://www.mysite.com/shirts/
The htaccess that runs the site is quite long and complex, so it's not easy to find or test which rule is causing the rewrite. I was able to track down the issue to this line of code (I think):
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
Does this rule match the behavior I'm describing above? It seems to be the cause, but it doesn't make logical sense to me. I don't unsderstand where the trailing slash is coming from.
Can someone shed some light on this for me? Thanks in advance.
Edit: MORE:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite\.com$
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
By default apache will add the ending /, you will have to use:
DirectorySlash Off
To disable that behavior which is caused by mod_dir, you can read more about it here.
However if you're trying to remove the / to fix images not showing. That is not the right way to do it, you should instead use the HTML base tag, for example:
<BASE href="http://www.yourdomain.com/">
Read more here about it.
Your current rule as you have updated on your question:
RewriteCond %{HTTP_HOST} ^mysite\.com$
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
Means:
if domain on the URL is only mysite.com
redirect current URL to domain with www.
So an example of it would be, if you access:
http://domain.com/blog/some_blog_article
It will redirect the user to:
http://www.domain.com/blog/some_blog_article
Note how it retains everything and only add the www. to the domain.
If you really want to redirect it regardless here is one way to do it:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
# check if it is a directory
RewriteCond %{REQUEST_FILENAME} -d
# check if the ending `/` is missing and redirect with slash
RewriteRule ^(.*[^/])$ /$1/ [R=301,L]
# if file or directory does not exist
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# and we still want to append the `/` at the end
RewriteRule ^(.*[^/])$ /$1/ [R=301,L]

.htacces mod_rewrite optional parameter

To have nice URLs I edited my .htacces file
domain.com/category/content
should be redirected to
domain.com/index.php?category=cat&content=cont
this works fine with this line
RewriteRule ^([^/]*)/([^/]*)$ /index.php?category=$1&content=$2 [L]
but
domain.com/category
should be redirected to
domain.com/index.php?category=cat
to display the contents of the whole category
I'm sure this is easy to solve but it is very confusing to me.
Use 2 rules:
RewriteRule ^([^/]+)/([^/]+)$ /index.php?category=$1&content=$2 [L]
RewriteCond %{REQUEST_URI} !/index.php
RewriteRule ^([^/]+)/?$ /index.php?category=$1 [L]

htaccess rule..not able to solve

i am struggling to get my site htaccess work... but no luck..
below is the code
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/? /profile.php?username=$1 [L,NC]
</IfModule>
this works fine no issues.. but when i add something it will not work.. in following cases -
1) For example when i type example.com/dinesh it will redirect me to www.example.com (always home page instead i want it to be www.example.com/dinesh
2) now i have another users.php with two parameters the thing is when i pass one parameter it should execute this rule profile.php but when i pass two parameters then it should take me to user.php i tried so many combination but this is not working.
if any expert can give me atleast some tips that will be great.
As for 1), it probably doesn't have anything to do with the above rules. If /dinesh exists, you should look in there. If it doesn't exist, you should look into /profile.php, that's probably what's redirecting you to the home page.
As for 2), your rule:
RewriteRule ^([^/]+)/? /profile.php?username=$1 [L,NC]
Matches the URI: /something/else, because the regular expression doesn't have an end-of-string match. It matches the first something, and that's good enough. If you add a $ to the end, it won't match /something/else.
RewriteRule ^([^/]+)/?$ /profile.php?username=$1 [L,NC]
Or, you can place the other rule that routes to user.php before the one that routes to profile. but it's better to have the $.

Remove subdirectory & remove .php extention in htaccess not working?

I m new to url rewrite:
First, here is what I am trying to accomplish:
Current URL: www.example.com/subdirectory1/subdirectory2/something.php
Desired URL: www.example.com/subdirectory1/something/
And, the name of subdirectory2 is fixed.
Possible?
My current htaccess just to remove the ".php" but also not working. (Any idea how to debug htaccess??)
RewritEngine on
RewriteCond %{REQUEST_URI} !\.php$ [NC]
RewriteCond %{REQUEST_URI} [^/]$
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^(?!subdirectory1/|subdirectory2/)(.+)$ subdirectory1/$1 [L]
Thanks.
Your first problem is RewritEngine on. You are missing an e. Should be RewriteEngine on.
Try this:
RewriteEngine on
# Remove .php
RewriteCond %{REQUEST_URI} \.php$
RewriteRule ^([^/]+)/fixed/([^/]+).php$ /$1/$2/ [R=301,L]
# Rewrite "friendly" URL into php.
RewriteRule ^([^/]+)/([^/]+)/?$ /$1/fixed/$2.php [L]
This only works for exactly what you said. Fixed is always the same. Replace it with the correct value.
The users goes to: www.example.com/1234/fixed/5678.php. He is redirected to www.example.com/1234/5678
User goes to www.example.com/1234/5678. On the server, this becomes www.example.com/1234/fixed/5678.php.
Something like www.example.com/1234/5678/9abcd will not work. (More than two levels of directories.)

Resources