Problem in Multiwrite Rule - mod-rewrite

U have used rewrite url module but not able to redirect to the target page and I am getting error as The requested URL /old.html was not found on this server.
Here is my code. Please see to that and suggest to me:
RewriteEngine On
RewriteCond %{SERVER_PORT} !^8080$
RewriteRule ^(.*)$ http://localhost/IN/$1 [L,R]
RewriteRule ^new.html$ /index.html$1 [L]

Your first rule will probably cause an infinite rule as the substitute URL doesn’t use the port 8080 neither. So try this:
RewriteCond %{SERVER_PORT} !^8080$
RewriteRule ^(.*)$ http://localhost:8080/IN/$1 [L,R]
You also need to request /new.html to see if your second rule works. Additionally, there is no first group in your pattern whose match can be referenced by $1. So:
RewriteRule ^new\.html$ /index.html [L]

Related

Redirect Subdomain to domain while preserving the path and query string

I have a subdomain and I want to redirect to the main domain (using .htaccess) like so:
https://abc.example.com I want to redirect it to https://www.example.com
https://abc.example.com/path/page-name to https://www.example.com/path/page-name
https://abc.example.com/path/page-name?test=12&test1=12 to https://www.example.com/path/page-name?test=12&test1=12
Please suggest how I can do it.
I have already tried the below solution but its not working.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$ [NC]
RewriteRule ^ http://www.example.com/suberror [L,R]
I am using Laravel.
At this line :
RewriteRule ^ http://www.example.com/suberror [L,R]
There is no pattern means , regex checked against requested URI .
Change it to this :
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R]
This part ^(.*)$ is pattern and it will be presented in substitution by this $1
If it is Ok , Change [L,R] to [L,R=301] to be permanent redirection because R alone means R=302 which is temporary .
Assumptions:
You have 1 subdomain (as stated in your example)
The subdomain and main domain point to the same area on the filesystem (they share a common root).
The try something like the following at the top of your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^abc\.example\.com [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [R,L]
The URL-path from the request is held in the REQUEST_URI server variable. The query string from the request is passed through to the substitution (target) without any additional work.

mod_rewrite forward shortend URL

I am looking for a way to create a short URL path for a longer URL on my page
the long url is: domain.com/tagcloud/user.html?t=1234ABCD
i would like to offer a short version of the URL to easy access it:
domain.com/t/1234ABCD
I tried a few examples but I just don't get it how I could forward these rules.
RewriteRule ^(.*)/t/$ /tagcloud/user.html?t=$1 [L]
I am also using MODX so they already use rules.
in addition my htaccess file
RewriteEngine On
RewriteBase /
# Always use www
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
I must keep the code snippets above in my htaccess file. The first one simply forwards http://domain.com requests to www.domain.com
The friendly URLs part is needed to translate the internal IDs of my CMS with the alias of the URL. This feature must remain because the entire site cannot be influencted by the changes I try to make in htaccess...
I simply would like to add a listener that only if the URL matches www.domain.com/t/abcd1234
Therefore I need something that identifies the www.domain.com/t/ URL
your help is much appreciated
Try this:
RewriteCond %{REQUEST_URI} ^/t/.*
RewriteRule ^t/(.*)$ /tagcloud/user.html?t=$1 [R=301,L]

mod rewrite and static pages

is possible to exclude a url being parsed by mod rewrite?
my .htaccess has rewrite rules like
RewriteRule ^contact contact_us.php
and a couple more static pages.
currently my site don't have troubles cause uses http://domain.com/user.php?user=username
but now i need rewrite to:
http://domain.com/username
I've tried with:
RewriteRule ^(.*)$ user.php?user=$1 [L]
but all my site stops working...
is possible to avoid parse my static pages like contact/feed/etc being treated like usernames?
edit to match david req:
this is my actual .htaccess file:
RewriteEngine On
Options +Followsymlinks
RewriteRule ^contact contact_us.php [L]
RewriteRule ^terms terms_of_use.php [L]
RewriteRule ^register register.php [L]
RewriteRule ^login login.php [L]
RewriteRule ^logout logout.php [L]
RewriteRule ^posts/(.*)/(.*) viewupdates.php?username=$1&page=$2
RewriteRule ^post(.*)/([0-9]*)$ viewupdate.php?title=$1&id=$2
RewriteRule ^(.*)$ profile.php?username=$1 [L]
also i've enabled modrewrite log my first file:http://pastie.org/1044881
Put the rewrite rules for the static pages first, and add the [L] flag to them:
RewriteRule ^contact contact_us.php [L]
...
then after those, use your rewrite rule for the username:
RewriteRule ^(.*)$ user.php?user=$1 [L]
(hopefully nobody has a username of contact).
EDIT: Based on the log output you posted (which I'm assuming corresponds to an unsuccessful attempt to access the contact page... right?), try changing the contact rewrite rule to either
RewriteRule ^contact$ contact_us.php [L]
or
RewriteRule ^contact contact_us.php [L,NS]
That is, either add $ to make the pattern match only the literal URL contact, or add the NS flag to keep it from applying to subrequests. According to the log output, what seems to have happened is that Apache rewrites contact to contact_us.php and then does an internal subrequest for that new URL. So far so good. The weird thing is that the ^contact pattern again matches contact_us.php, "transforming" it to contact_us.php, i.e. the same thing, which Apache interprets as a signal that it should ignore the rule entirely. Now, I would think Apache would have the sense to ignore the rule only on the subrequest, but I'm not sure if it's ignoring the entire rewriting process and leaving the original URL, /contact, as is. If that's the case, making one of the changes I suggested should fix it.
EDIT 2: your rewrite log excerpt reminded me of something: I'd suggest making the rewrite rule
RewriteRule ^([^/]+)$ user.php?user=$1 [L]
since slashes shouldn't be occurring in any usernames. (Right?) Or you could do
RewriteRule ^(\w+)$ user.php?user=$1 [L]
if usernames can only include word characters (letters, numbers, and underscore). Basically, make a regular expression that matches only any sequence of characters that could be a valid username, but doesn't match URLs of images or CSS/JS files.
The -f and -d options to RewriteCond check if the current match is a file or directory on disk.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ....

mod_rewrite - redirect from one domain to another and preserve trailing values in url

I think this is a pretty straight forward question in mod_rewrite:
I got one domain, which needs to redirect to another, but keep any value after last slash (/) in the first URL, over to the second.
domain.com/4433 should transfer to domain.com/folder/?p=4333
Listed for clarity:
From: domain.com/4433
To: domain.com/folder/?p=4333
Any ideas?
Edit:
Did some testing, we found the following solution:
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^([0-9a-z]*)$ /folder/?p=$1 [NC]
sincerely,
- bakkelun
In case you don't really want to redirect but to have pretty URLs, you can use
RewriteEngine On
RewriteRule ^/(.+)$ /folder?p=$1 [L]
This takes everything after the first slash and inserts it at the $1 - but only if there's something after the slash. It doesn't issue a redirect so the users won't notice.
Without any further information, try this:
RewriteEngine on
RewriteRule ^/([^/]+)$ /folder/?p=$1
If you want to use the rule in a .htaccess file, remove the leading slashes.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$ domain.com/folder?p=$1 [R=301,L]
Just in case: domain.com = domain1.com and domain2.com? domain1.com should be redirected to domain2.com? Both run on the same server (optional)?
[EDIT:]
If you really only want to do the thing as stated in the comment, then do the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1.com$
RewriteRule ^4433$ http://domain2.com/folder/?p=4433 [R=301,L]
Else, as Benedikt Eger said, or with R=301 if you want real redirection.
Or, if you want it to redirect only on numbers, then do the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1.com$
RewriteRule ^([0-9])+$ http://domain2.com/folder/?p=$1 [R=301,L]
RewriteCond checks, if defined vhost is domain1.com, but not domain2.com, then the rewrite rule is applied, and redirects via HTTP status 301 [R=301] only number strings (0-9)+ consisting of at least one number to the specified URL. [L] makes this the last rule applied.

Simple mod rewrite question

Here is my current .htaccess file:
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
As you can see, requests to http://domain.com go to http://domain.com/index.html. I want to change this so that they go to http://domain.com/foo, please note that does not exist as a file or folder, it is handled by rails. How do I do this? Note that I have tried the following and it doesn't work:
RewriteRule ^$ foo [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
Thanks!
You're probably safe to just change the first line to this:
RewriteRule ^$ foo [QSA,L]
The L flag tells mod-rewrite that it shouldn't apply any other rules after that one. The problem right now is that the second rule gets applied after the first one, and you end up at "foo.html", instead of "foo", right?
The difference between you trying to send to "foo" and the original redirect to "index.html" is that the second rule applies to requests that do not include a period. So when the first rule was redirecting to "index.html", after it was used, the second rule was no longer valid. However, now that you're not redirecting to a location with a period in it, the second rule gets applied after the first one, so you get a double-redirect.
In addition, you may be able to drop the QSA flag from the first line, it depends on your site though. If someone accesses the site like http://domain.com/?user=fred, do you want to send them to http://domain.com/foo?user=fred, or just http://domain.com/foo? If you don't need the Query String Appended, you can drop the QSA flag, and just have:
RewriteRule ^$ foo [L]

Resources