.htaccess RewriteRule slash to get parameter - .htaccess

I just transfered my Domain to a new Server. Mod_Rewrite is enabled on the new server but unfortunately some RewriteRules don't work, while others do. I haven't changed anything in the .htaccess
So the URL www.mydomain.com/go/10.html should make an internal redirect to www.mydomain.com/go.php?name=10
The snippet in the .htaccess looks like this:
# go.php
RewriteRule ^go$ "$0/" [R=301,L,QSA]
RewriteRule ^go/$ go.php [L,QSA]
RewriteRule ^go/.*?([^\.\/]*)\.html$ go.php?name=$1 [L,QSA]
The $_GET["name"] is not available if I call this url.

Replace your .htaccess code with this.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^(go)/([^.]+)\.html$ /$1.php?name=$2 [L,QSA,NC]

Related

How do I redirect in .htaccess file without changing URL

I want to redirect several pages to a PHP page with GET variables. However, when I try to use an .htaccess file, it redirects but changes the URL. I don't want it to change the URL structure when redirecting. How would I do this?
For example, I want to redirect /fly-snooze-cruise to http://example.com/package_listing.php?package-type=fly-snooze-cruise
and /fly-snooze-cruise/orlando-airport to http://example.com/package_listing.php?package-type=fly-snooze-cruise&airport=orlando-airport
Here is my .htaccess code:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^fly-snooze-cruise$ http://www.example.com/package_listing.php?package-type=fly-snooze-cruise [L]
RewriteRule ^fly-snooze-cruise/orlando-airport$ http://www.example.com/package_listing.php?package-type=fly-snooze-cruise&airport=orlando-airport [L]
RewriteRule ^fly-snooze-cruise/orlando-sanford-airport$ http://www.example.com/package_listing.php?package-type=fly-snooze-cruise&airport=orlando-sanford-airport [L]
RewriteRule ^fly-snooze-cruise/melbourne-airport$ http://www.example.com/package_listing.php?package-type=fly-snooze-cruise&airport=melbourne-airport [L]
RewriteRule ^snooze-park-cruise$ http://www.example.com/package_listing.php?package-type=snooze-park-cruise [L]

Pull querystring from cgi-bin with mod_rewrite and redirect

I have a file path that looks like this: /cgi-bin/folder/script.cgi?t=1&u=http://domain.com/url/url-2/
What I want to do is create a rewrite rule to pull the querystring u= and then redirect to that URL, I've been working for a while on this with no success.
RewriteRule ^cgi-bin/folder/script.cgi?t=1&u=(.*)$ $1 [R=301,L]
Above is an example of one of my "solutions" that obviously didn't work. Any help is much appreciated. Thanks!
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 %{THE_REQUEST} ^[A-Z]{3,}\s/+cgi-bin/folder/script\.cgi\?t=1&u=([^\s&]+) [NC]
RewriteRule ^ /%1 [R=302,L,NE]

Rewrite all files from folder to another folder

I want redirect all requested files from folder-a to folder-b. E.g. http://www.yoursite.com/folder-a/index.php to http://www.yoursite.com/folder-b/index.php.
How can I do that? I have verified if mod_rewrite works with this code:
RewriteEngine On
RewriteRule ^google.html$ http://www.google.com/ [R=301]
The structure on the webspace is the following:
.htacces
|-folder-a
|-folder-b
But if I want my folder redirect
RewriteEngine On
RewriteRule ^/folder-a/(.*)$ http://www.yoursite.com/folder-b/$1 [L,R=301]
the redirect doesn't work if I input the following URL:
http://www.yoursite.com/folder-a/
http://www.yoursite.com/folder-a/index.php
The redirect doesn't take place. I stay on the same page ... What I'm doing wrong? I also tried it with this htaccess:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^folder-a(.*)$ http://www.yoursite.com/folder-b$1 [L,R=301]
</IfModule>
If all this works I want to exclude some files. Eg. with this htaccess:
RedirectPermanent /folder-a/info.php /folder-b/new-info.php
Edit:
Now I tried this htaccess
redirectMatch 301 ^/folder-a/ http://www.yoursite.com/folder-b
This works, but I need something which takes the whole path and rewrite it to the new folder.
This for example doesn't work:
RewriteRule ^folder-a/(.*)$ folder-b/$1
Solution
I had an old htacess file in my folder-a so the redirect didn't worked. This is my final htaccess:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^folder-a/excludefile1.php http://www.yoursite.com/folder-b/newnameforfile1.php [L,R=301]
RewriteRule ^folder-a/excludefile2.php http://www.yoursite.com/folder-b/newnameforfile2.php [L,R=301]
RewriteRule ^folder-a/(.*)$ http://www.yoursite.com/folder-b/$1 [L,R=301]
</IfModule>
You probably need to put both rules in place: one for the empty folder and one for other files, and then the catch-all at the bottom. This is not tested:
RewriteEngine On
RewriteRule ^folder-a/info.php http://www.yoursite.com/folder-b/new-info.php [L,R=301]
RewriteRule ^folder-a/ http://www.yoursite.com/folder-b/ [L,R=301]
RewriteRule ^folder-a/(.*)$ http://www.yoursite.com/folder-b/$1 [L,R=301]

Having much trouble with mod_rewrite

I'm having some problems in my .htaccess file.
I would like to display the content of this URL:
http://mywebsite.com/admin/payments/invoices/view/index.php?id=123456
When I access this one:
http://mywebsite.com/admin/payments/invoices/view/123456/
Here's my actual .htaccess
Options +FollowSymlinks
RewriteEngine On
RewriteBase /admin/payments/invoices/
RewriteRule ^/view/([0-9]+)/(index.php)?$ /view/index.php?id=$1 [L,QSA]
Do you have any idea?
Thanks!
If you do have the view directory, then the easiest thing is to put this .htaccess in that directory (i.e. {DOCUMENT_ROOT}/admin/payments/invoices/view/.htaccess):
Options +FollowSymlinks
RewriteEngine On
RewriteBase /admin/payments/invoices/view
RewriteRule ^(\d+)$ index.php?id=$1 [L,QSA]
The index.php in the left side hand of the RewriteRule is not required (actually, I expect it not to work: the DirectoryIndex should not yet have been injected in the URI at this stage - unless some other RewriteRule is in effect?), nor is the / at the end of the RewriteBase.
I tested the above on Apache/2.2.21, but the module rules are the same for later versions.
Try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^.*admin/payments/invoices/view/index\.php\?id=([0-9]+) admin/payments/invoices/view/$1/index.php [L]
</IfModule>
Try putting this in the htaccess file in your document root:
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)id=([0-9]+)
RewriteRule ^admin/payments/invoices/view/index\.php$ /admin/payments/invoices/view/%1/index.php [L]
So when you enter http://mywebsite.com/admin/payments/invoices/view/index.php?id=123456 in your browser's URL address bar, you will get served the content at /admin/payments/invoices/view/123456/index.php. Since it's completely unclear what you actually want, in case you wanted it the other way around:
RewriteEngine On
RewriteRule ^admin/payments/invoices/view/([0-9]+)/(index.php)?$ /admin/payments/invoices/view/index.php?id=$1 [L,QSA]

.htaccess: Redirect root url to subdirectory, but keep root URL structure

I have created a site in a subdirectory and would like the site to appear as if it's in the root.
I used the below Mod_Rewrite code to get the site root to redirect to the subdirectory, but I would like the folder the files are held in to not appear.
currently: www.example.com/sitefiles/content/
Would like: www.example.com/content
Thanks
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteRule ^$ /sitefiles/ [L]
RewriteRule (.*) /sitefiles/$1 [L]
If it needs to be done via .htaccess and mod_rewrite, then use this:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/sitefiles/
RewriteRule (.*) /sitefiles/$1 [L]
Since you do not explicitly specify the page when website root will be hit, then there is no need for this line: RewriteRule ^$ /sitefiles/ [L]
You need to add condition for your main rewrite rule to prevent rewrite loop (when already rewritten URL gets rewritten again): RewriteCond %{REQUEST_URI} !^/sitefiles/
Well, the directory should have no reason to appear in any listing, and that mod_rewrite you posted should allow you to do as you said. Still, I would go about it a different way:
Options +FollowSymLinks +Indexes
RewriteEngine on
RewriteBase /
RewriteRule ^$ sitefiles/ [L]
RewriteRule ^(.+)$ sitefiles/$1 [L]
Hope this works.

Resources