Rewrite numbers to default page - mod-rewrite

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.

Related

mod_rewrite to extensionless file results in 404

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.

Silly 404 page usage?

Alright, so the only way I know of changing links to not show the file extensions;
this yourwebsite.com/customlink.php to yourwebsite.com/customlink
is either creating a folder called customlink and shoving an index file in there. Or creating a 404 page which snoops around the URL and grabs whatever string is behind the last / and does whatever to show the proper content.
My question is if this way of solving the problem is straight out idiotic, or not? I'm doing this because in my mind it saves space, let me explain: Every page that needs a customlink are the same with some bits and content taken from other places, so instead of creating X amount of folders and index files that includes a main file, I'll just have one 404 file that can handle it.
I apologize in advance if this is really stupid
As you are not mentioning what server your pages are running on (Apache, IIS, ...?), I'll just assume Apache.
Put an .htaccess file into the root of your site with the following content:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
It will internally rewrite everything without an extension to the corresponding php file, provided that:
the request is not a valid directory
a file with a php extension is in fact present
A more common approach is to route all HTTP requests to a single index.php using mod_rewrite in a .htaccess.
Then, based on the requested resource, the index.php outputs the appropriate file, whether it is generated from a database or nested in some other folder.
It's probably not a good idea to use a 404 page (an error page) for anything other than "File Not Found" instances.

URL Rewriting all subsites to main site

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.

Advice for Getting mod_rewrite to Work

I am trying to make clean URLs. I have written a simple rule on the .htaccess file and I have been told that it should work. However, it does not. I was wondering if it might have something to so with mod_rewrite not being enabled.
I am using Network Solutions shared hosting. I called the company and was told that mod_rewrite is enabled by default on the hosting I have.
Any advice on how I can check to see if mod_rewrite is enabled? Also, I would appreciate any related advice.
Thanks in advance,
John
EDIT: I just posted this at Serverfault.com, but I'm hesitant to delete it here since I have a feeling it might get more views here. Let me know if I should delete it here as well. Thanks.
EDIT:
Below is my .htaccess file. I am getting a 404 error when I go to a URL that this should re-write.
RewriteEngine On
RewriteRule ^comments/([A-Za-z0-9-]+)-([0-9]+)?$ index.php?submissionid=$2 [NC,L]
Try putting up a bad .htaccess file (i.e. random text) in a test folder. You should get all 500 errors from that folder. If not, .htaccess files are not enabled, and you'll have to get your hosting company to switch them on.
Then try a really simple rewrite rule:
RewriteEngine On
RewriteRule foo index.php
and see if that works. If you get a 500 error at this stage, mod_rewrite is probaby not enabled.

using mod_rewrite to redirect from subdomain to maindomain

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).

Resources