Jump to content

Recommended Posts

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?

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?

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!

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?

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!

 

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!

 

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?

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.

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?

 

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.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.