JsusSalv Posted July 15, 2009 Share Posted July 15, 2009 I use the following regex to remap my links to the homepage (index.php): RewriteRule ^([a-zA-Z0-9_-]+)/?$ index.php [NC,L] RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ index.php [NC,L] RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ index.php [NC,L] It works well. However, I don't like having to write a bunch of these ([a-zA-Z0-9_-]+)/ to accommodate for every subdirectory. Is there a regex to dynamically account for each subdirectory without having to hardcode? Or a better code that will remap all links (not css, js, or images) to the index.php file? Quote Link to comment https://forums.phpfreaks.com/topic/166005-mod_rewrite-help-making-url-rewrite-rule-dynamic/ Share on other sites More sharing options...
JsusSalv Posted July 16, 2009 Author Share Posted July 16, 2009 I know Wordpress uses the following: # BEGIN WordPress <ifmodule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </ifmodule> # END WordPress But that doesn't work for me with regard to subdirectories. Any suggestions on what I can do? Quote Link to comment https://forums.phpfreaks.com/topic/166005-mod_rewrite-help-making-url-rewrite-rule-dynamic/#findComment-876153 Share on other sites More sharing options...
JsusSalv Posted July 16, 2009 Author Share Posted July 16, 2009 Here's what I'm trying to accomplish: 1) all links that look like domain.com/xxxxx are redirected to the homepage (index.php) file. 2) all links that reference a subdirectory, domin.com/xxxx/xxxx/xxxx are also redirected to the homepage (index.php) file. NOTE: The subdirectories are real at this point but will more than likely be a mixture or real and virtual be changed later on. Confusing? Perhaps but that's where it is now. My first code works just fine...but only if I hard code extra ([a-zA-Z0-9_-]+)/ for each subdirectory. I don't want to harcode anything. I'd like to use a regex to handle the thinking for me and just accept anything that comes after the .com and redirect to index.php. I appreciate any help. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/166005-mod_rewrite-help-making-url-rewrite-rule-dynamic/#findComment-876159 Share on other sites More sharing options...
JsusSalv Posted July 18, 2009 Author Share Posted July 18, 2009 I found this at http://www.askapache.com/htaccess/apache-htaccess.html ## SEO REWRITES RewriteRule ^(.*)/ve/(.*)$ $1/voluntary-employee/$2 [L,R=301] RewriteRule ^(.*)/hsa/(.*)$ $1/health-saving-account/$2 [L,R=301] I am not sure if this could be rewritten to help accomplish my goal. Can someone give me some pointers? Quote Link to comment https://forums.phpfreaks.com/topic/166005-mod_rewrite-help-making-url-rewrite-rule-dynamic/#findComment-877520 Share on other sites More sharing options...
JsusSalv Posted July 19, 2009 Author Share Posted July 19, 2009 Perhaps my question should be...IS it possible to do something like this? Quote Link to comment https://forums.phpfreaks.com/topic/166005-mod_rewrite-help-making-url-rewrite-rule-dynamic/#findComment-878122 Share on other sites More sharing options...
lifetalk Posted July 25, 2009 Share Posted July 25, 2009 I have not tested it, but this 'might' work: RewriteRule ^/(.*)$ "http\:\/\/www\.domain\-domain\.com\/index\.php" [R=301,L] Quote Link to comment https://forums.phpfreaks.com/topic/166005-mod_rewrite-help-making-url-rewrite-rule-dynamic/#findComment-882880 Share on other sites More sharing options...
JsusSalv Posted July 25, 2009 Author Share Posted July 25, 2009 Thank you for the idea. It didn't work however. I can see what it should have done but it's giving me this instead: http://www.domain-domain.com/index.php Do you have another suggestion I could try? Thank you for your time and help! Quote Link to comment https://forums.phpfreaks.com/topic/166005-mod_rewrite-help-making-url-rewrite-rule-dynamic/#findComment-882909 Share on other sites More sharing options...
lifetalk Posted July 26, 2009 Share Posted July 26, 2009 Is that not what you want? I mean, you want every instance of subdirectory request rewritten/redirected to index.php, right? Quote Link to comment https://forums.phpfreaks.com/topic/166005-mod_rewrite-help-making-url-rewrite-rule-dynamic/#findComment-883112 Share on other sites More sharing options...
JsusSalv Posted July 26, 2009 Author Share Posted July 26, 2009 Is that not what you want? I mean, you want every instance of subdirectory request rewritten/redirected to index.php, right? Yes, of course. But the code you provided results in this: http://www.domain-domain.com/index.php What I need is for any of the following: (with or without the 'www') http://www.domain.com/xxxxx http://www.domain.com/xxxxx/xxxxxx http://www.domain.com/xxxxx/xxxxxx/xxxxxxx http://www.domain.com/xxxxx/xxxxxx/xxxxxx/xxxxxx to redirect to the homepage file, which is index.php This: http://www.domain-domain.com/index.php doesn't do that. I did learn something new from it but it just appends an extra '-domain' to the 'domain.com'. I copied the code letter for letter...no modifications except for the 'domain' part. I used to do all of this using variables in the URL but I need to change that. Hence, my plea for help on this forum. The code I posted at the top of this thread does exactly what I need but it doesn't dynamically take x number of directories into account. I have to manually create an extra line for every directory I want redirected to the index.php file. Too cumbersome I think. I was hoping to find a way to make it dynamic...without having to hardcode all the extra line. Should I modify your code in any way so it doesn't append an extra '-domain' to domain.com? Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/166005-mod_rewrite-help-making-url-rewrite-rule-dynamic/#findComment-883115 Share on other sites More sharing options...
lifetalk Posted July 26, 2009 Share Posted July 26, 2009 Obviously! My code was an example, in case you had an extra '-' in your domain. Because that '-' needs to be escaped, thus the complete example. Modified: RewriteRule ^/(.*)$ "http\:\/\/www\.domain\.com\/index\.php" [R=301,L] Quote Link to comment https://forums.phpfreaks.com/topic/166005-mod_rewrite-help-making-url-rewrite-rule-dynamic/#findComment-883124 Share on other sites More sharing options...
JsusSalv Posted July 26, 2009 Author Share Posted July 26, 2009 I removed my code and replaced it with your line. Seems to redirect properly. However, for some reason I'm getting a 'The requested URL /blog/archive was not found on this server' error. Would I need to modify anything else? Quote Link to comment https://forums.phpfreaks.com/topic/166005-mod_rewrite-help-making-url-rewrite-rule-dynamic/#findComment-883129 Share on other sites More sharing options...
lifetalk Posted July 26, 2009 Share Posted July 26, 2009 Is that error an issue with every subdirectory? Quote Link to comment https://forums.phpfreaks.com/topic/166005-mod_rewrite-help-making-url-rewrite-rule-dynamic/#findComment-883138 Share on other sites More sharing options...
JsusSalv Posted July 26, 2009 Author Share Posted July 26, 2009 Yes for all subdirectories and for top level links as well. So /blog/archive AND /about both give the same issue. There isn't anything else in my htaccess file that deals with rewrite rules or conditions. It's strange because /blog/archive AND /about work fine with the regular super long code I wrote so I know they resolve to the proper locations. Your code has been the simplest answer I've read regarding this idea. Thank you for it! Any other ideas on what I need to do? Quote Link to comment https://forums.phpfreaks.com/topic/166005-mod_rewrite-help-making-url-rewrite-rule-dynamic/#findComment-883144 Share on other sites More sharing options...
lifetalk Posted July 26, 2009 Share Posted July 26, 2009 Spare me... Guess lack of sleep got the better of me when I first suggested that code: This SHOULD work: RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ "http\:\/\/www\.domain\.com\/index\.php" [R=301,L] Basically, the redirection rule will not apply if the request is made to a file that physically exists on the server. So, for now, if you type: http://www.domain.com/woot/woot/woot/woot/1/2/3/4/woot/woot.html The code above will redirect you to: http://www.domain.com/index.php HOWEVER.. if the file woot.html exists in the above example, it will ignore the rule. Quote Link to comment https://forums.phpfreaks.com/topic/166005-mod_rewrite-help-making-url-rewrite-rule-dynamic/#findComment-883149 Share on other sites More sharing options...
JsusSalv Posted July 26, 2009 Author Share Posted July 26, 2009 LOL! Yes,that certainly works. However, every URL not only redirects to index.php but stays as index.php. /about is now index.php /blog/archive is now index.php Quote Link to comment https://forums.phpfreaks.com/topic/166005-mod_rewrite-help-making-url-rewrite-rule-dynamic/#findComment-883158 Share on other sites More sharing options...
lifetalk Posted July 26, 2009 Share Posted July 26, 2009 Not really sure what you meant there. Forgive me If I get it correct, you mean that /blog/archive is a functional page that you DON'T want redirected to index.php? Quote Link to comment https://forums.phpfreaks.com/topic/166005-mod_rewrite-help-making-url-rewrite-rule-dynamic/#findComment-883318 Share on other sites More sharing options...
JsusSalv Posted July 26, 2009 Author Share Posted July 26, 2009 I think I'm beginning to see why there is such limited information for doing something like this via mod_rewrite. My original intention was to have any link I create redirect a user to the homepage, which is index.php. All links look like the following: domain.com/about domain.com/blog/archive etc... I used to make links look like this: domain.com/about?id=8... OR domain.com/blog/archive?article=1&image=5... But I've gotten away from that as it is too messy and ugly. So I figured, if the variables are for pulling db info. Why not just use links that look like real links, instead of ugly non-SEO style links. Within index.php I have a PHP script that grabs the $_SERVER['REQUEST_URI'], looks for the last '/' character in the URI and uses any value after the forward slash in a database query. So... /about becomes --> SELECT * FROM table WHERE page = 'about'; If it's a subquery... /blog/archive --> SELECT * FROM table WHERE article = 'blog' AND year = 2008; You get the picture. So the URI provides all the variables for creating an SQL statement. The last code your provided does in fact redirect a user, upon clicking any link, to the homepage file (index.php). But the URI never changes from index.php and therefore won't pull the correct db info for a particular page. Yes, I THOUGHT I wanted all links to redirect to the index.php file. But it seems like it may not be as simple. Thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/166005-mod_rewrite-help-making-url-rewrite-rule-dynamic/#findComment-883436 Share on other sites More sharing options...
lifetalk Posted July 26, 2009 Share Posted July 26, 2009 OK.. I see. So your URI currently provides the data needed to build an SQL query and return the content from the database. If that's the case, then what is it that you want done? Forgive the blunt question, but I'm a bit confused now Quote Link to comment https://forums.phpfreaks.com/topic/166005-mod_rewrite-help-making-url-rewrite-rule-dynamic/#findComment-883461 Share on other sites More sharing options...
JsusSalv Posted July 27, 2009 Author Share Posted July 27, 2009 Well, I'm confused myself. That's why I am looking for help. This code: RewriteRule ^([a-zA-Z0-9_-]+)/?$ index.php [NC,L] works just fine for links that look like this: <a href="/home" title="Home">Home</a><br /> <a href="/blog" title="Blog">Blog</a><br /> <a href="/portfolio" title="Portfolio">Portfolio</a><br /> <a href="/about" title="About">About</a><br /> <a href="/contact" title="Contact">Contact</a> But if I wanted links that looked like this: <a href="/home" title="Home">Home</a><br /> <a href="/blog/archive" title="Archive">Archive</a><br /> <a href="/portfolio/gallery1" title="Gallery 1">Gallery 1</a><br /> <a href="/about/owner" title="About The Owner">About The Owner</a><br /> I would need to write the following: RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ index.php [NC,L] If there were MORE subdirectories I would need to write additional lines for these links: <a href="/home" title="Home">Home</a><br /> <a href="/blog/archive/2008" title="2008">2008</a><br /> <a href="/portfolio/gallery1/subgallery-a" title="Subgallery A">Subgallery A</a><br /> So it would look like this: RewriteRule ^([a-zA-Z0-9_-]+)/?$ index.php [NC,L] RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ index.php [NC,L] RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ index.php [NC,L] This method makes me take into account every subdirectory and therefore have to write a new RewriteRule for each subdirectory. Your code did exactly what I was looking for. But I hadn't realize that it would force the URI to point to index.php for every link instead of what was actually within the <a href=""></a> tag. It's not a very dynamic system if I have to go in and write a new RewriteRule for any new subdirectories I create in the future. I wish I could just make it dynamic so it would just accept any subdirectories without me having to go in and create new RewriteRules all the time. I'm sorry for the confusion. I'm not new to mod_rewrites or regex but this one is making me want to take a full semester of intro to Mod_Rewrites. It seems so simple yet so complex at the same time. I appreciate all your help! Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/166005-mod_rewrite-help-making-url-rewrite-rule-dynamic/#findComment-883787 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.