.htaccess redirect according to PHP parameters - apache

I'm trying to build some redirections with my .htaccess file to deal with some old forum url. I want the redirections to be made according to the PHP parameters (the topic ID).
For instance, something like
RewriteEngine On
RedirectPermanent /forum/viewtopic.php?t=123 /page1.html
RedirectPermanent /forum/viewtopic.php?t=345 /page7.html
RedirectPermanent /forum/viewtopic.php?t=89 /page3.html
The old and new URL are not related to each other (no PHP parameter has to be kept or something). I want to decide manually in my .htaccess file what to do for each topic ID.
But I can't manage to do that so easily. I tried many things, but nothing works.
Is this possible ? Any idea ?
Thanks a lot !
Edit : additional question : I want to add a global redirection of all the folder /forum to the root of the site ("/"). I guess I can place it after the others, so if no other rule is trigered, this one will be trigered.
I'm trying some things like
RewriteRule ^forum /? [L,R=301]
But everything I have tried so far redirects me to the "page1.html" (my first rule). Any idea why ? Thanks a lot !

You can't match against the query string using mod_alias's Redirect, RedirectMatch, etc. You need to use mod_rewrite and match against the %{QUERY_STRING} variable:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^t=123$
RewriteRule ^forum/viewtopic\.php$ /page1.html? [L,R=301]
RewriteCond %{QUERY_STRING} ^t=345$
RewriteRule ^forum/viewtopic\.php$ /page7.html? [L,R=301]
RewriteCond %{QUERY_STRING} ^t=89$
RewriteRule ^forum/viewtopic\.php$ /page3.html? [L,R=301]
NOte that RewriteEngine is a mod_rewrite directive, not mod_alias. So it has no affect at all on the RedirectPermanent directives.

Related

Apache re-write URL path that doesn't exist

We currently have a website with a URL structure as follows:
https://www.example.com/en_CA/homepage/page1.html
https://www.example.com/en_CA/homepage/page2.html
We need to shorten the URL to:
https://www.example.com/page1.html
https://www.example.com/page2.html
We have tried using the following rewrite rules and conditions:
RewriteCond %{REQUEST_URI} !^/en_CA/homepage/ [NC]
RewriteRule ^/(.*)$ /en_CA/homepage/$1 [P]
The problem we have is that we get a 404 because the shorter URL doesn't exist. I think the solution needs to also involve AliasMatch to set up an alias for that URL but I'm not sure how to go about that.
I've tried:
AliasMatch ^/[^/]*/(.*) /en_CA/homepage/$1
RewriteCond %{REQUEST_URI} !^/en_CA/homepage/ [NC]
RewriteRule ^/(.*)$ /en_CA/homepage/$1 [PT]
But this doesn't work.
The website is build using Adobe AEM so we need to ensure that AEM only ever receives the long URL.
Thanks
Russell
There is no need to use AliasMatch, I think, you want to access the url https://www.example.com/en_CA/homepage/page1.html from https://www.example.com/page1.html, and the same to the other one. Please let me know if I am wrong.
Try this, let me know if it works:
Please read the comments (text after # symbol) carefully
# Add this to your root .htaccess file i.e the public_html, htdocs, etc. or use RewriteBase /
RewriteEngine On # Do not use this two times in one .htaccess file, be sure you don't have any other directories other than /en_CA/homepage/ in your root dir, or use the RewriteRule ^(.*)$ /dirname/$1 [L] for every dir.
RewriteCond %{REQUEST_URI} !^/en_CA/homepage/
RewriteRule ^(.*)$ /en_CA/homepage/$1 [L]
I am sure the above will work.
Follow the same to other folders.
I will add something later to hide the folder containing it.

Apache mod_rewrite for specific folders and paths

I have found dozens of articles online on how to setup mod_rewrites but for the love of God I can't figure out how to PROPERLY force HTTPS on ALL pages and after that force HTTP on certain directories or (already rewritten) pages.
Now this one gets really tricky as I need HTTPS on this directory, except for two cases, such as "/surf" which actually is rewritten from "surf.php", and "promote-([0-9a-zA-Z-]+)$" which is rewritten from "promote.php?user=$1" :
<Directory /home/rotate/public_html/ptp/>
AllowOverride None
Order Deny,Allow
Allow from all
Options +SymLinksIfOwnerMatch
ErrorDocument 404 "<h1>Oops! Couldn't find that page.</h1>"
RewriteEngine On
RewriteRule ^promote-([0-9]+)$ promote.php?user=$1 [QSA,NC,L]
RewriteRule ^([^.?]+)$ %{REQUEST_URI}.php [L]
</Directory>
I have tried some stuff but which only resulted in some weird redirection loops...
RewriteCond %{HTTPS} on
RewriteRule !^(surf|promote-([0-9]+)$) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
So basically I need to force HTTPS everywhere in /ptp/ except /ptp/surf (which is rewritten from surf.php AND /ptp/promote-123 which is rewritten from promote.php?user=123
Currently I'm using PHP to redirect to HTTP or HTTPS as per my needs but I know that it would be much faster if I could manage to do it via rewrites.
Any pointers, tips, suggestions? Thanks.
UPDATE2: This worked:
RewriteCond %{HTTPS} off
RewriteRule !^(surf|promote(-[0-9]+)?) https://%{HTTP_HOST}%{REQUEST_URI} [R=301]
RewriteRule ^promote-([0-9]+)$ promote.php?user=$1 [NC,L]
RewriteRule ^([^.?]+)$ %{REQUEST_URI}.php
However, the resources such as javascript, fonts etc, were being blocked by the browser, unless I specified absolute HTTPS paths. Note that this never happened when redirecting through PHP...
I changed a little bit and it works perfectly
Changes
Remove the Change the RewriteRule to match file to .php to bottom.
Remove the $ sign that is End of the pattern
As Said in the update promote-1111 will redirect to promote.php?user=$1 change the promote-[0-9]+ to promote(-[0-9]+)? otherwise it will override in the second redirection as you redirecting it to promote.php?user=$1
The Code
RewriteCond %{HTTPS} off
RewriteRule !^(surf|promote(-[0-9]+)?) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^([^.?]+)$ %{REQUEST_URI}.php [L]
The page surf
The Page Index
Never mind the error message shown in this image. Since I tried it from my localhost, it won't have a certificate.
Will work with servers
Your rules aren't in the "update" working because of side effects of using <Directory> context. Each substitution starts processing again.
When you request /promote-123 and rewrite it to put the numbers in the query string, you can't then match the numbers as if they're still in the path. You'll need to match the rewriten path and the numbers with RewriteCond %{QUERY_STRING} (if you care about the numbers)

Apache Rewrite for Entire Subdomain

How would I go about rewriting :
proxmox.example.com
to
example.com/forward/?url=proxmox.example.com
I am trying to do this on Ubuntu with Apache
Please let me know what Apache plugins are used.
Will this rewrite go into my VirtualHosts file? (/etc/apache2/sites-available)
Thanks for the help!
You won't need a vhost for that. It can be put in the main Apache config.
Put this in your config:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(proxmox.(example.com))$
RewriteRule .* http://%2/forward/?url=%1 [R,L]
Use [P,L] instead of 'R' if you want to mask/hide the rewrite from the user. Otherwise stay with 'P'.
Also I used two nested brackets to retrieve example.com (value is stored in %2) and use that instead of a static 'example.com' in the second line. But if you prefer a 'clearer' (=easier to read) rule, use this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(proxmox.example.com)$
RewriteRule .* http://example.com/forward/?url=%1 [R,L]
Hope it helped. Let us know if it did, and even more so, if it didn't so we can help furthermore :)

Simple mod_rewrite RewriteRule for messy legacy url's

Launching a new website for a new client. Their old site has about 50 products and unfortunately, the old product names do not match up to the new URL pattern
Old URL Examples:
example.com/products.aspx?category=Foo&product=SuperLongNoBreakProductNameIDDescription
example.com/products.aspx?category=Foo&product=ProductNameDescription&var1=1293.123
example.com/products.aspx?category=Bar&product=ProductCategoryProdNameRandomNumbers
(The old URL's are sometimes hitting 150+ characters.)
New URL's:
example.com/products/category/actual-product-name
There's no set, recognizable pattern to go from the old product name to the new one. There is for the category.
I've tried simple mod_alias Redirects, but understand that I need a RewriteRule instead. But I'm having problems. All I need is a 1-to-1 redirect for each of these 50 URL's. I thought I could do something like:
RewriteRule ^/products.aspx?category=Foo&product=ProductName
/products/category/new-product-name/ [R=301,NC]
But that isn't working. I know this should be simple, but I am stuck. Any ideas?
Use the pattern below for the rest of your redirect urls. Note that you escape special characters e.g. ? , . and space by adding a \ in front of them
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /products\.aspx\?category=Foo&product=SuperLongNoBreakProductNameIDDescription [NC]
RewriteRule ^ /products/category/new-product-name/ [R=301,NC]
Have a look at the RewriteMap directive of mod_rewrite.
You can specify in a text file something like:
products.aspx?category=Foo&product=SuperLongNoBreakProductNameIDDescription /products/category/new-product-name
And in your httpd.conf
RewriteMap productmap /path/to/map/file.txt
RewriteRule ^(.*) ${productmap:$1} [R=301,NC]
Tip: If it's a permanent redirect you want, make sure you set an appropriate Cache-Control and Expires header to instruct browsers to cache the 301.
You can try something like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^category=Foo&product=ProductName$
RewriteRule ^products\.aspx$ /products/category/new-product-name/? [R=301,L]
Notes:
In per-dir (.htaccess) context, the per-dir prefix is stripped, so you can't start the RewriteRule pattern with ^/.
You have to use RewriteCond to match against the query string.
As stated in another answer, a RewriteMap solution might be suited to this situation, if you have access to httpd.conf / the vhost definition for this site. I'm not sure how that works with query strings though.
For something like this, it might be a better solution to rewrite all of these URLs to a server side script, and use the script to do the HTTP redirect for each URL.

Apache .htaccess RewriteRule

Here's my situation. I have a web root and several subdirectories, let's say:
/var/www
/var/www/site1
/var/www/site2
Due to certain limitations, I need the ability to keep one single domain and have separate folders like this. This will work fine for me, but many JS and CSS references in both sites point to things like:
"/js/file.js"
"/css/file.css"
Because these files are referenced absolutely, they are looking for the 'js' and 'css' directories in /var/www, which of course does not exist. Is there a way to use RewriteRules to redirect requests for absolutely referenced files to point to the correct subdirectory? I have tried doing things like:
RewriteEngine on
RewriteRule ^/$ /site1
or
RewriteEngine on
RewriteRule ^/js/(.*)$ /site1/js/$1
RewriteRule ^/css/(.*)$ /site1/css/$1
But neither of these work, even redirecting to only one directory, not to mention handling both site1 and site2. Is what I'm trying possible?
EDIT: SOLUTION
I ended up adapting Jon's advice to fit my situation. I have the ability to programatically make changes to my .htaccess file whenever a new subdirectory is added or removed. For each "site" that I want, I have the following section in my .htaccess:
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteCond %{HTTP_COOKIE} sitename=site1
RewriteCond %{REQUEST_URI} !^/site1/
RewriteRule ^(.*)$ /site1/$1 [L]
Index.php is a file that lists all my sites, deletes the "sitename" cookie, and sets a cookie of "sitename=site#" when a particular one is selected. My RewriteConds check,
If the request is not for /
If the request is not for /index.php
If the request contains the cookie "sitename=site1"
If the request does not start with "/site1/"
If all of these conditions are met, then the request is rewritten to prepend "/site1/" before the request. I tried having a single set of Conds/Rules that would match (\w+) instead of "site1" in the third Condition, and then refer to %1 in the fourth Condition and in the Rule, but this did not work. I gave up and settled for this.
If the RewriteRules are in your .htaccess file, you need to remove the leading slashes in your match (apache strips them before sending it to mod_rewrite). Does this work?
RewriteEngine on
RewriteRule ^js/(.*)$ /site1/js/$1
RewriteRule ^css/(.*)$ /site1/css/$1
EDIT: To address the comment:
Yes, that works, but when I do RewriteRule ^(.*)$ /site1/$1, it causes Apache to issue internal server errors. But to me, it seems like that should just be a generic equivalent of the individual rules!
What's happening with that rule is when /something/ gets rewritten to /site/something/, and apache internally redirects, it gets rewritten again, to /site/site/something/, then again, then again, etc.
You'd need to add a condition to that, something like:
RewriteCond %{REQUEST_URI} !^/site/
RewirteRule ^(.*)$ /site/$1 [L]
You need to set up symlinks, which the rewrite rules will use so your absolute links at the server level can follow the symbolic links to the central site hosting account.