0

I have no issues with setting up the mod_rewrite and having it create the clean URLs. My question is for more experienced htaccess users and I am wondering if there is a simple solution to my dilemma.

My URLs without the cleanup look like:
index.php?ctrl=portal&mode=main
or
index.php?ctrl=portal&mode=edit&id=2

I want my URLs to look like:
http://www.mydoamin.com/portal/main
or
http://www.mydoamin.com/portal/edit/2

I can easily have the clean URL with the ctrl/mode; my issue is, how can I setup the rewrite so that if the id (id is only an example, this var could be anything) will only append to the URL if it is set?

currently I am using

# Turn on RewriteEngine
RewriteEngine on
RewriteBase /

# Do a www redirect on the domain
RewriteCond %{HTTP_HOST} ^test\.dev [NC]
RewriteRule ^(.*)$ http://www.test.dev/$1 [R=301,L]

# Make sure it's not a file or a folder before we attempt a redirect
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 

# Perform main redirect
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)?$ index\.php?ctrl=$1&mode=$2 [L]

# If loading just the index, redirect to /
RewriteCond %{THE_REQUEST} ^GET\ .*/index\.(php|html)\ HTTP
RewriteRule ^(.*)index\.(php|html)$ /$1 [R=301,L]

The ctrl and mode will always be in the URL, sometimes there will be more vars passed. I would like each of these vars to be appended to the URL via "/". IE: /portal/main or /portal/edit/2 or /user/manage/2/contacts. I hope this is explained with enough detail.

Does anyone know any good methods to achieve this functionality? I am not an expert in htaccess but I am no beginner either. Any other ideas or suggestions would be much appreciated.

Thanks

2
Contributors
3
Replies
5
Views
7 Years
Discussion Span
Last Post by djcodesa
0

Also, the extra params won't always be available so I can't just force the redirects with extra params, I somehow need the htaccess to know how many params there are and build the link accordingly.

Is this possible or would doing rewrite conditionals for each and every link type there is and have the redirect work off of that?
Like:

RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)?$ index\.php?ctrl=$1&mode=$2 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)?$ index\.php?ctrl=$1&mode=$2&action=$3 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)?$ index\.php?ctrl=$1&mode=$2&action=$3&sub=$4 [L]

I certainly hope there is a better way than that.

Thanks

0

Ok, this was my solution for this issue. If anyone has a better method please let me know.

Here's the part of my htaccess to do this functionality.

# Turn on RewriteEngine
RewriteEngine on
RewriteBase /

# Make sure it's not a file or a folder before we attempt a redirect
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 

############
# Direct Friendly URLs
############

# 4 vars
RewriteCond %{REQUEST_URI} ^(.*)/(.*)/(.*)/(.*)$
RewriteRule ^(.*)/(.*)/(.*)/(.*)$ /index\.php?ctrl=$1&mode=$2&action=$3&call=$4 [L]

# 3 vars
RewriteCond %{REQUEST_URI} ^(.*)/(.*)/(.*)$
RewriteRule ^(.*)/(.*)/(.*)$ /index\.php?ctrl=$1&mode=$2&action=$3 [L]

# 2 vars
RewriteCond %{REQUEST_URI} ^(.*)/(.*)$
RewriteRule ^(.*)/(.*)$ /index\.php?ctrl=$1&mode=$2 [L]

############
# END - Direct Friendly URLs
############

It's simple to see how it works; I check the condition for the vars being passed and adjust the URL accordingly.

Hope this is helpful to someone.

0

So then how do you know what the variable is?

Do you just look at the "position" of the param in the URL to determine what the variable value is?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.