How to show always the same url? - regex

I am using Apache.
I have www.example.com/?p_action=user_profile&post_author=34 and I want to use www.example.com/niceurl/
I want that if user enters any of the urls, the www.example.com/niceurl/ is shown in his browser (of course, www.example.com/?p_action=user_profile&post_author=34 is actually retrieved from the server).
RewriteRule does not seem to be the solution for this, does it?
Thanks

Well you can try putting,
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /?p_action=$1&post_author=$2 [L]
In your .htaccess file.
IMO RewriteRule should solve your problem.

Related

.htaccess to change directory name with querystring

I have a url like
domain.com/order/index.php?q=
the order folder no longer exists as its been changed to ecom, so it now looks like the below.
domain.com/ecom/index.php?q=
The problem is we have a ton of external links wanting the order url some scripts and images pull from this url, etc.
How can I simply set .htaccess to still allow the order directory.
I tried this
RewriteRule ^order/(.*)$ /ecom/$1 [L]
It works only if it exactly matches order (e.g domain.com/order/ , but anything after order it then breaks (e.g domain.com/order/index.php?q=)
Any help would be appreciated. I just keep seeing examples similar to what I have above so not sure if I am doing something wrong here.
Use QSA flag
RewriteRule ^order/(.*)$ /ecom/$1 [QSA,L]
you can do a permanent redirect to a page
RewriteRule ^url-string-to-redirect$ http://www.yourdomain.com/your-new-url-string [R=301,L]

.htaccess to rewrite to sub directory - preserving page

My directory setup is as follows:
Root directory at what.ever.ip (/var/www/)
User's sites are what.ever.ip/username
Users root ftp are /var/www/username/upload_files_here,
which translates to what.ever.ip/username/upload_files_here
So if user Foo uploads the file index.html he'll have to visit what.ever.ip/Foo/upload_files_here/index.html
I want to use apache .htaccess to rewrite this (without redirecting aka showing it) to display as what.ever.ip/Foo/index.html
I have tried multiple solutions without finding one that solve my problem.
So my question is how do I rewrite "what.ever.ip/username/upload_files_here/index.html" to "what.ever.ip/username/index.html" without showing it?
PS. The what.ever.ip is not known, could be an local address, localhost or an remote address. So I would prefer a possible wildcard instead of hard coding in "what.ever.ip"
Thanks, Alex.
Place this rule in /var/www/.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/((?!upload_files_here/).*)$ /$1/upload_files_here/$2 [L,NC,QSA]
The following:
RewriteEngine On
RewriteRule ^(.*)/upload_files_here/(.*)$ /$1/$2 [L]
should change:
what.ever.ip/username/upload_files_here/index.html to what.ever.ip/username/index.html while showing still what.ever.ip/username/upload_files_here/index.html in the URL bar.

htaccess redirect 301 with forward slash in parameters won't work

I have done some research so far, but could not find the solution. I have a client who has this old file structure on the URL:
/index.php?process=views/article.php&articleId=44102
which should redirect to this URL:
/news/title-of-article/
I found that I may need this in my htacces file:
RewriteCond %{QUERY_STRING} &articleId=44102 [NC]
RewriteRule .* /news/title-of-article/ [R=301,L]
and when I test it with the full URL, it won't redirect. But when I leave out the forward slash like this:
/index.php?process=viewsarticle.php&articleId=44102
it redirects fine. So I assume the forward slash is the problem here.
I'm not a specialist in setting up .htaccess files and this drives me crazy, because it seems that I have the right solution in plain sight, but don't know why the forward slash is preventing the redirect.
If you can't answer with the final solution, I'd appreciate to have some kind of tool (website, software, etc) where I would be able to test these rewrite conditions in detail. Maybe there is something like the regex-testers that are available for regular expressions, but just for RewriteCond and RewriteRules that I can use to figure it out myself.
Thanks in advance
Marian
Problem is that you're not stripping out QUERY_STRING In rewritten URI and because of that resulting URL also contains QUERY_STRING as process=views/article.php&articleId=44102 and it matches rule again causing redirection loop.
Change your rule to this:
RewriteCond %{QUERY_STRING} &articleId=44102 [NC]
RewriteRule .* /news/title-of-article/? [R=301,L]
? in the end will strip out existing query string.

How can I redirect all files in a folder to another directory except a few files using mod_rewrite?

I have a situation where an entire folder's contents are no longer needed and will be redirected to the home page, except 6 or so files. The folder holds over 300 files, so individual redirects:
redirect 301 /folder/file.html http://www.domain.tld/
redirect 301 /folder/file2.html http://www.domain.tld/
redirect 301 /folder/file3.html http://www.domain.tld/
This would take quite a long time. I have some time before needing this done, and would like to know if anyone knows a good way to achieve this by using a little regex with mod_rewrite.
For optimum understanding for all who may use the potential correct answer, lets say the files that we don't want to redirect are:
/folder/stay1.html
/folder/stay2.html
/folder/stay3.html
Thanks in advance for this wonderful community of very knowledgeable people helping those of us who still have a few things to learn!
Edit
Is it possible to achieve this and keep the base url of the folder?
/folder/
/folder/index.html
I tried the following without success:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/folder(/|/index.html|/stay1.html|/stay2.html|/stay3.html|/etc.html)
RewriteRule ^/?folder/ http://www.domain.tld/ [L,R=301]
Edit Correct Answer
A big thanks goes out to Jon Lin for the answer.
The correct method to redirect all files of a /folder/ except a few, while still allowing access to /folder/ is:
RewriteEngine On
# Allow /folder/ to remain accessible
RewriteCond %{REQUEST_URI} !^/folder/$
# Allow specified files to remain accessible
RewriteCond %{REQUEST_URI} !^/folder/(index.html|stay1.html|stay2.html|stay3.html|etc.html)
# Redirect all non-specified files to home page
RewriteRule ^/?folder/(.+)$ http://www.domain.tld/ [L,R=301]
Using mod_rewrite, you can create exception conditions to the redirect, try putting these rules in the htaccess file in your document root:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/folder/$
RewriteCond %{REQUEST_URI} !^/folder/(index.html|stay1.html|stay2.html|stay3.html|etc.html)
RewriteRule ^/?folder/(.+)$ http://www.domain.tld/ [L,R=301]
So anything that's in the list: (stay1.html|stay2.html|stay3.html|etc.html) will fail the condition and the redirect won't happen. Otherwise, anything starting with /folder/ will get redirected to http://www.domain.tld/.
Note that if you have mod_alias redirects intermixed, they may interfere with each other.
You could use RedirectMatch with a negative lookahead, like:
RedirectMatch permanent ^/?folder/(?!(stay1\.html|stay2\.html|stay3\.html)) http://domain.tld
An alternative mod-rewrite solution would be like this:
RewriteRule ^/?folder/(stay1\.html|stay2\.html|stay3\.html)$ - [L]
RewriteRule ^/?folder/.* http://domain.tld
The first rule catches all the exceptions, the L flag ensures no further processing takes place in this pass, and the - instructs the engine not to rewrite, ensuring no further passes are made. Anything not caught by the first rule is redirected by the second rule.

Temporary redirect 302 with .htaccess and mod-rewrite matching expression

I'm trying to match a a bunch of redirects for my website with basically moved to a different folder on the server. I need to make http://www.site.com/index.php?page=anypage go to http://www.site.com/newfolder/index.php?page=anypage. The thing is http://www.site.com/index.php and http://www.site.com/index.php?page=home should remain untouched. How can I accomplish this?
I was trying the following in the .htaccess file, but I am affraid to make a mistake. I really don't know how to test this, either.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^/index.php?page=(.*)$ http://www.site.com/newfolder/index.php?page=$1 [R=302,NC]
RewriteRule ^/index.php?page=home http://www.site.com/index.php?page=home [R=302,NC,L]
Now I figured that this is temporary, so I should know ho to reverse it! The next week, the links will have to redirect again to the root server. Also, what should I do to re-establish the normal redirection??
If I've followed your scenario correctly, you want something like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} !=""
RewriteCond %{QUERY_STRING} !page=home
RewriteRule ^index.php /newfolder/index.php [R,L]
As far as testing goes, I prefer to try rules out on a local test server. If you have full control over the server (as is the case locally), there are some mod_rewrite directives that help you log what's going on, and that can be helpful in debugging. The module documentation has more information about this.
Edit: When you want to switch back, modify the RewriteRule above like so:
RewriteRule ^newfolder/index\.php /index.php [R,L]