mod_rewrite - replace word or character in url - mod-rewrite

Am trying to mod_rewrite a URL, but unfortunately without any luck.
http://mywebsite.com/gallery/mycustom-gallery/linkid417
Should be changed to:
http://mywebsite.com/gallery/mycustom-gallery/#417
Where '417' is a dynamic id of an image & mycustom-gallery will also change every-time.
I've tried the following rules, but none seems to work...
RewriteRule ^/gallery/$1/#([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/y/([0-9]+)$ /gallery/$1/linkid [L,R=301]
RewriteRule mycustom-gallery/#.*$ /mycustom-gallery/linkid=417/$1
Regards,
Charl

If you are trying to rewrite from http://mywebsite.com/gallery/mycustom-gallery/linkid417 to http://mywebsite.com/gallery/mycustom-gallery/#417 you can do it as follows:
RewriteRule ^/gallery/([a-zA-Z0-9_-]+)/linkid([0-9]+)$ /gallery/$1/#$2 [NE,L,R=301]
The NE|noescape flag prevents Apache from escaping the hash in the URL during the 301 redirect.
If however you wish to do the opposite, i.e. redirect from http://mywebsite.com/gallery/mycustom-gallery/#417 to http://mywebsite.com/gallery/mycustom-gallery/linkid417, that cannot be done, as the fragment part of the request ("#417") is not passed to Apache along with the request as per RFC1808.

Related

replace character in rewrite rule

Here are my current rules:
RewriteCond %{QUERY_STRING} ^to=(one|seventeen|thirty\+four)
RewriteRule ^/folder/page.php$ http://www.site.com/folder/category/%1? [L]
RewriteRule ^folder/category/(.+)\+(.+)$ http://www.site.com/folder/category/$1-$2 [L]
The first rule works fine, it redirects perfectly if the word is in the query string, but I can't get thirty+four to become thirty-four when redirected.
Any help would be greatly appreciated.
For starters, RewriteRule ^/folder/page.php$ will never match anything. The URI's get the prefix (the leading slash) removed if the rules are in an .htaccess file instead of server config.
Secondly, since you've included http://www.site.com/ in your targets, that means the browser will get redirected instead of internally rewritten. You need to remove http://www.site.com/ from your first rule so that the second one can be applied.
Here's what should work:
RewriteCond %{QUERY_STRING} ^to=(one|seventeen|thirty\+four)
RewriteRule ^folder/page.php$ folder/category/%1 [NC,QSA,L]
RewriteRule ^folder/category/(.+)\+(.+)$ folder/category/$1-$2 [NC,QSA,L]
And now three hints:
1)
Please make sure you've read everything here before asking:
Here's the wiki of serverfault.com
The howto's htaccess official guide
The official mod_rewrite guide
2)
Please try to use the RewriteLog directive: it helps you to track down problems:
# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On
3)
My favorite tool to check for regexp:
http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)
You use this tool when you want to check the URL and see if they're valid or not.

Need help removing part of filename from url with mod_rewrite

I'm trying to reformat my url to be a bit shorter. Right now the links end up as this: website.com/image?id=name.jpg
What I want to have the link come out as is m.website.com/name, without the file exension or image.php file in the url. I figure mod_rewrite is the way to do it, so any help will be greatly appreciated.
In order to make it so someone accessing the URL http://m.website.com/name gets served the content for http://website.com/image?id=name.jpg, you first need to check the hostname for m.website.com, then match the name part of the URI. Using that match, you can proxy the request (using a [P]) or, if both website.com and m.website.com are hosted on the same server, just simply internally rewrite. Try putting this in your .htaccess file in your document root:
RewriteEngine on
# check the host (NC = no case)
RewriteCond %{HTTP_HOST} ^m\.website\.com$ [NC]
# don't rewrite /image
RewriteCond %{REQUEST_URI} !^/image
# Match the first non-slash word and rewrite
RewriteRule ^([^/]+)$ /image?id=$1 [L]
This will rewrite http://m.website.com/name to /image?id=name.jpg, but it will not rewrite http://m.website.com/path/name. If you want paths (and everything else) to be included in the id parameter, change the ([^/]+) to (.*) in the RewriteRule.

always preserve URL when redirecting to subfolder

I want to redirect all requests to a certain path on my server (/app) to a subdirectory at /app/app_site. Following rewrite rules do the job for requests like 'http://localhost/app/somepage.htm':
RewriteCond %{REQUEST_URI} !^/app/app_site.*$
RewriteCond %{REQUEST_URI} !^/app_site.*$
RewriteRule ^/app(.*) /app/app_site$1 [L,PT]
This results in the correct page, while preserving the URL. Also, 'http://localhost/app/' will fetch the index page at /app/app_site/index.html, while preserving the URL 'http://localhost/app/'.
However, when I enter 'http://localhost/app', following things happen:
the correct page is fetched, at /app/app_site/index.html
yet, the URL is redirected to 'http://localhost/app/app_site/'
I'm nearly there, but would like to preserve the URL in all cases (also those without trailing slash). Anyone have a clue how to do this? Thanks!
This is the expected behaviour with DirectorySlash enabled, because you've rewritten to a directory that lacks a trailing slash, and mod_dir performs this cleanup after you've rewritten the URL with mod_rewrite.
The easiest solution is to rewrite the URL so that it always at least matches the slash-terminated directory path, like so:
RewriteCond %{REQUEST_URI} !^/app/app_site.*$
RewriteCond %{REQUEST_URI} !^/app_site.*$
RewriteRule ^/app/?(.*)$ /app/app_site/$1 [L,PT]
This prevents mod_dir from having to add the trailing slash, and therefore avoids the external redirection to /app/app_site/ you're experiencing now.

mod_rewrite issues

I have used the mod_rewrite module but was not able to redirect to the target page - I am getting an error:
The requested URL /old.html was not found on this server.
Rewrite rules as follows:
RewriteEngine On
RewriteRule ^/IN/index.html$ /IN/index.iface [L]
You need to request a URL with a path that’s matched by the pattern of your RewriteRule directive. So in your case obviously /IN/index.html (where the . can actually be any character, as it’s not escaped).

Apache RewriteRule to subdirectory

I need to redirect myhomepage.com/ to myhomepage.com/sub/
When I read the guide at apache.org/docs/1.3/misc/rewriteguide.html I have no clue what they are talking about. Hence I decided to friendly ask one of the experts here. I guess it takes just some seconds to figure that rule out.
Thanks, Carin.
Try this rule:
RewriteRule !^sub/ sub%{REQUEST_URI}
It will redirect any requested URL path that does not start with /sub/ (!^sub/) internally to /sub/ (sub%{REQUEST_URI}).
This rule is for the .htaccess configuration file in your document root. If you want to use it in your httpd.conf, prepend the pattern with a /.
And if you want an external redirect, prepend the substition with a / too and add the [R] flag:
RewriteRule !^sub/ sub%{REQUEST_URI} [R]
If you only want to redirect http://myhomepage.com/ (i.e. you don't need to redirect http://myhomepage.com/example.html to http://myhomepage.com/sub/example.html), the rule is as simple as:
RewriteRule ^$ http://myhomepage.com/sub/ [R=301,L]

Resources