[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 285
  • Last Modified:

mod_rewrite: Rewrite http://mydomain.com/a1.a2 to http://mydomain.com/script.php?var1=a1.a2

Hello,

How can I mod_rewrite the following:

http://mydomain.com/a1.a2 

to

http://mydomain.com/script.php?var1=a1.a2


I've been trying to do it but I keep getting the actual script.php file rewrited as well, so I suppose the rewrite would have to specifically ignore the rule if it's a .php file after the slash, but I don't know how to do that.

Thanks!
0
NeedExpertHelp
Asked:
NeedExpertHelp
  • 5
2 Solutions
 
jb1devCommented:
Like this:

RewriteEngine On
RewriteRule ^/(\w*)\.(\w*)$ /script.php?var1=$1.$2 [L]

Open in new window

0
 
jb1devCommented:
> I keep getting the actual script.php file rewrited as well,

Oh wait, you are doing a remote rewrite?
Now I see what you mean. Can be more specific as to what "a1" might match and we can attempt to exclude the .php script with the regex?
0
 
jb1devCommented:
This is better

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/script.php
RewriteRule ^/(\w*)\.(\w*)$ /script.php?var1=$1.$2 [R]

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
NeedExpertHelpAuthor Commented:
Thanks jb1dev.

I see what you're trying to do there, but it's not fully working.

What does the (\w*) do (e.g. the 'w')?

Anyway, the other problem is that that would also forward the homepage to the script, which I don't want.

Also, is a RewriteCond necessary, or can it be done in a one line RewriteRule?

For example, I tried the following:

RewriteRule ^(.*[^\.php][^\.css])$ /script.php?var=$1

Which seemed to work, but in reality is excluding the letters (p,h,c,s) and not the extensions (php, css).

Any other ideas?


0
 
jb1devCommented:
> What does the (\w*) do (e.g. the 'w')?

\w indicates a word character. Like a letter or number. The * means 0 or more.

> Anyway, the other problem is that that would also forward the homepage to the script, which I don't want.

Right if your homepage is called index.html you get script.php?var1=index.html

You could exclude that with more RewriteConds

> For example, I tried the following:
> RewriteRule ^(.*[^\.php][^\.css])$ /script.php?var=$1
> but in reality is excluding the letters (p,h,c,s)

Right, the brackets [ ] are creating classes or sets contains in the first case . (dot) , p, and h, and in the second case . (dot) c and s. The redundant letters are ignored.

E.g. [abcd] would match a, b, c or d. [^abcd] would be not a, b, c, or d.

Maybe you want something like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/.*\.php
RewriteCond %{REQUEST_URI} !^/.*\.css
RewriteRule ^/(\w*)\.(\w*)$ /script.php?var1=$1.$2 [R]

You can either restrict in the RewriteCond patters which you don't want to match (By using "!" before the regex which would match) or you can be more selective with the match in your RewriteRule. In your example you use a1.a1. If it's s single alphabetic character followed by a digit, a dot and then the first two again you could easily match that with
\w\d\.\w\d That would not matching index.html, but would match "v4.f9"












0
 
jb1devCommented:
> Also, is a RewriteCond necessary, or can it be done in a one line RewriteRule?

I'm not sure that you can exclude matches like that in a single regex. But we could simplify the above RewriteCond to somethig like this for the "do not match" cases.

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(.*\.php)|(.*\.css)
RewriteRule ^/(\w*)\.(\w*)$ /script.php?var1=$1.$2 [R]

0
 
arober11Commented:
You could try:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond  %{REQUEST_URI}   ^/([a-zA-Z0-9]*\.[a-zA-Z0-9]*)$
RewriteRule ^.*  /script.php?var1=%1  [L]

Note: The RewriteBase is optional / not required if the rules are in a httpd.conf

See also:

http://www.experts-exchange.com/Web_Development/Internet_Marketing/Search_Engine_Optimization_SEO/Q_24826725.html
0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now