I'm new with URL Rewriting and dont know how to do this:
All sites after /ex1 should be redirect to index.php
My current script:
RewriteEngine On
RewriteRule ^(ex1|ex2)/?$ index.php [NC,L]
But this will only rewrite example.com/ex1/and not example.com/ex1/abcd
Thanks in advance ;)
Close to perfect, try this instead:
RewriteEngine On
RewriteRule ^(ex1|ex2)/? index.php [NC,L]
Note: the trailing $ in the regular expression has been removed. That one was the cause why only the exact urls example.com/ex1 and example.com/ex1/ were matched, but no longer ones. The trailing $ anchors the search to the line end, so the end of the url here. So if there is anything between the last matched characters (the ex1 or ex1/ in this case), then the expression will not match.
Another more general note: this pattern is only usable withing .htaccess style files, but not in the normal hosts configuration. There are situations when such files come in handy, for example if you do not have administrative control over the http server. But in general you should always prefer to place such things inside the http servers hosts configuration instead of using .htaccess style files. Reason is that such files are notoriously error prone, hard to debug and finally really slow the server down.
Related
On my server I have an image cache where all the image files are saved with the hash of their original location as their file name, without extension. I want to serve the images via mod_rewrite, with their original filename at the end of the URL.
For example, the physical location of an image file on the server is
/_img_cache/a/3/a3a37b602a1df1addfc8f2ff5a9824e1c9bd5273
I want to serve this image with the url
https://example.com/_img_cache/a/3/a3a37b602a1df1addfc8f2ff5a9824e1c9bd5273/OriginalFilename.jpg
So, I added this to my .htaccess file:
RewriteEngine On
RewriteRule ^_img_cache\/([a-z0-9])\/([a-z0-9])\/([a-z0-9]+)\/.+$ _img_cache/$1/$2/$3 [L]
It should just strip off the original filename part and serve me the file.
I've tried this, but it gave me a 404 in return.
Unable to find my mistake, I made some slight modifications, added an extension to my test file, and changed the RewriteRule accordingly:
/_img_cache/a/3/a3a37b602a1df1addfc8f2ff5a9824e1c9bd5273.jpg
RewriteEngine On
RewriteRule ^_img_cache\/([a-z0-9])\/([a-z0-9])\/([a-z0-9]+)\/.+$ _img_cache/$1/$2/$3.jpg [L]
If I request the same URL, I get my image served without problems. But it's not really the way that I wanted. I want it to work with the files as-is; extensionless. And that seems to be the only variable causing it to fail.
Why is Apache claiming the file without an extension, after rewrite, does not exist, while I've verified multiple times that it does. Is it a bug or a feature? Or am I missing something else? :)
Try this adjustment:
RewriteRule ^(_img_cache/([[:xdigit:]])/([[:xdigit:]])/\2\3[[:xdigit:]]{38})/.+ $1 [END,DPI]
Refer to the flags documentation, which warns that L can cause looping, which is rarely what people want. It should usually be combined it with DPI, and END is preferable if you don't need to check the rewritten path against any other rules.
Here's the explanation for the simplified regex.
I am trying to rewrite a condition that can take any page that has numbers.html, lets say the like the following:
12345.html
33443.html
234545656.html
9797423.html
and just redirect it to index.php
RewriteRule ^([0-9]+)$ index.php [L]
I tried the following but it doesn't work, any help is very much appreciated.
Two immediate issues with your code:
you have to switch on the rewriting engine prior to using it and
if you want to match file names ending with .html, then you have to code that.
Have a try with this:
RewriteEngine on
RewriteRule ^([0-9]+)\.html$ index.php [L]
Then a few more hints:
the code you tried will only work in .htaccess style files. But the usage of such files has to be enabled first in your http server configuration.
a .htaccess style file has to be placed at the correct location, in this example within the folder actually holding the index.php file.
the rewriting module has to be installed and enabled, without those commands will not be available.
And a general remark:
Those .htaccess style files are notoriously error prone, hard to debug and they really slow down the http server, often for nothing. You should always prefer to place such commands inside the real host configuration of your http server. .htaccess style files are only offered as a last option for situations where you have no access to that configuration, for example when using a really cheap shared hosting provider.
Unfortunatly I am forced to use IIS6 for my MVC website. .net 4.0 adds functionality to mimic the stupid hack for getting extensionless urls to work in IIS6. My website is designed to take advantage url rewriting for SEO purposes. The keyword urls that the client wishes to use dictate an elaborate url rewriting scheme.
The problems with Microsofts impelmentation of the feature really comes down to url rewriting and the attempt to match a pattern. I have seen various rules that attempt to strip the dreaded eurl.axd from the url so that the patterns will match. I attempted to use these rules
RewriteRule ^(.)/eurl.axd/[a-f0-9]{32}(.)$ $1$2
or
RewriteRule (.)eurl.axd/. $1
which does work but it also introduces other problems when there are nested redirects. i.e. handling old urls to new ones, etc
what happens is the eurl.axd gets stripped and on the redirect the isapi_filter doesnt get the request which results in an IIS 404 errror.
Tinkering around with the urls, i came up with this possible solution.
RewriteRule ^generators/generator-parallel-capability/([^/])/([^/])$ /generators/htmlcontent/generator-parallel-capability/$1/$2 [NC,L]
it just grabs the eurl.axd portion and rewrites it to the executing url with it appended.
Is there a better way? I have several hundred urls that meet this pattern and it would be nice to have a single rule handle them all.
We used one generic rule on top of the config to cut the /eurl.axd234234
RewriteRule ^(.)/eurl.axd.$ $1 [NC]
this must work for everything but the root.
Using the rewrite rule RewriteRule ^(.)/eurl.axd.$ $1 [NC] results in some unplesant behavior when the url is rewritten more than one time. i.e. from an old url to the new vanity url then to the actual execution url.
Using maps, I was able to produce a pattern that works quite nicely and keeps the .htaccess file from being cluttered.
RewriteCond ${contentmap:$2} >"" [NC]
RewriteRule ([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /$1/${contentmap:$2}/$2/$3/$4 [NC,L]
RewriteCond ${contentmap:$2} >"" [NC]
RewriteRule ([^/]*)/([^/]*)$ /$1/${contentmap:$2}/$2 [NC,L]
The first detects the pattern /controller/some-content/eurl.axd/1234 and rewrites it to /controller/some-controller-action/eurl.axd/1234
the second does the same thing just without the eurl junk at the end. this is for my dev machine running iis 7
Im sure there are better ways and I am certainly open to better suggestions.
I have the following rules in my htaccess:
RewriteRule ^([^/.]+)/?$ list.php?categoryShortForm=$1&locationShortForm=world [QSA]
RewriteRule ^([^/.]+)/([^/.]+)/?$ list.php?categoryShortForm=$1&locationShortForm=$2 [QSA]
RewriteRule ^([^/.]+)/([^/.]+)/[^/.]*-p([0-9]+)/?$ view.php?categoryShortForm=$1&locationShortForm=$2&postingId=$3 [QSA]
In my localhost (windows, xampp), it all works fine.
In my real server (linux, apache) the first 2 rules work fine, but not there 3rd one.
For example:
/plastic-surgery/california-usa/ works fine, but
/plastic-surgery/los-angeles-california-usa/test-1-p1 gives me a 404
Any idea??
Check to make sure that you can browse directly to the target URLs. If mod_rewrite is rewriting onto something that doesn't exist, you'll get that 404. It might help to ratchet up mod_rewrite's log level to a high value, so you can see what it's rewriting to.
I am a little unclear on how your working URL is supposed to actually work. All three of your patterns start with "one or more non-slash, non-period characters" ([^/.]+), but URLs going into the pattern matching start with slashes: "/plastic-surgery/california-usa/".
Have you turned on mod_rewrite logging and checked out what mod_rewrite is actually doing?
RewriteLog "/tmp/rewrite.log"
RewriteLogLevel 9
My problem is that i have a functioning subdomain (sub.mydomain.com). sub is loaded in mydomain.com/sub.
What i would like to do is to redirect all requests to sub.mydomain.com to mydomain.com.
Somehow it seems that when im the subdomain i cannot access the rootfolder (main domain). I can get it working with from mydomain.com/sub to mydomain.com. But not from the subdomain.
Currently im using:
RewriteEngine on
RewriteRule ^(.*)/?$ /home/web/webuser/$1 [L]
When accessing sub.mydomain.com i get a 500 Internal Server Error.
Is there a restriction in accessing the main from a sub? (rights wise)
Or maybe another way of getting to main, perhaps something like (../$1)
Thanks
EDIT:
I only have access to .htaccess. So DocumentRoot cannot AFAIK be used in .htaccess file.
What about symlinks? I dont really know what it does, but i assume that it links two locations? The only code i found for that enables symlinks (Options +FollowSymlinks) - but this line doesnt say anything about what to link (perhaps im all wrong)
Btw. thanks for input so far !
I must admit that I did not fully understand your question. Do you want to redirect everything from sub.mydomain.com/whatever to mydomain.com/whatever? In that case, the following (put in the config file of your sub.mydomain.com) might work:
RewriteEngine On
RewriteRule ^/(.*)$ http://mydomain.com/$1 [R,L]
It redirects on the client side, meaning that the user will see mydomain.com/sub in the browser.
EDIT: I think I understand your question now. Yes, it's a permissions issue: If the DocumentRoot of your web site is /whatever/sub, then you cannot just access /whatever by adding "/.." to the URL. I hope you understand that this is a good thing. :-) mod_rewrite just changes the URL, so it cannot do that either.
So, to solve your problem, you need to either change the DocumentRoot of sub.mydomain.com or create a symlink that allows you to access the required directory (e.g. /whatever/sub/redir-target -> /whatever). Be careful with the symlink option, though, since it will create valid directories of infinite length on your file system (/whatever/sub/redir-target/sub/redir-target/...) and some scripts cannot cope with that.
EDIT2: Instead of a symlink, you might want to create an alias, e.g., something like this:
Alias /redir-target /home/web/webuser
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/redir-target/.*$
RewriteRule ^/(.*)$ /redir-target/$1
Still, I think the easiest solution is to change the DocumentRoot...
Why not try using a Redirect Directive from mod_alias?
It's difficult to provide a definitive answer without knowing more about your server configuration.
The following might work and is at the very least a decent starting point:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub\.mydomain\.com
RewriteRule (.*) /$1 [L]
Ideally that would go in your httpd.conf, but might work from a .htaccess file (again, more information about how your subdomains are setup would be helpful).