0

I am creating a rule to display addresses of type :

main.php?cat=MyCategory
main.php?cat=MyCategory&sub=MySubcategory
main.php?cat=MyCategory&sub=MySubcategory&prod=MyProduct

As

MyDomain.com/MyCategory
MyDomain.com/MyCategory/MySubcategory
MyDomain.com/MyCategory/MySubcategory/MyProduct

but there are two problems:

The first problem is that the other links on the displayed page start in the wrong position.

MyDomain.com/MyCategory/MySubcategory/MyProduct/newlink.html

should be referenced to the root, like this

MyDomain.com/newlink.html

Second problem is that from 2nd/3rd URL levels, my references such as javascript, img src and css links are wrong. It looks for these files relative from the up levels url. How do I fix this?

My .htaccess file

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ principal.php?cat=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ principal.php?cat=$1&sub=$2 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ principal.php?cat=$1&sub=$2&prod=$3  [L]

Tried with:

RewriteCond %{REQUEST_URI} !^/newlink\.html$ 

But not working, any ideas?

2
  • Where is your .htaccess located? Do you have more than .htaccess in your system? Commented Oct 17, 2022 at 10:40
  • htaccess is located in root dir Commented Oct 18, 2022 at 5:06

1 Answer 1

2

For your first query to deal with 4th level .html format URLs try following .htaccess rules file. Make sure to clear your browser cache before testing your URLs.

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:[^/]*/[^/]*/[^/]*/)([^.]*\.html)/?$  $1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ principal.php?cat=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ principal.php?cat=$1&sub=$2 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ principal.php?cat=$1&sub=$2&prod=$3  [L]

For JS/CS rewrite/redirect: You may need to use base tag to fix your js and other relative resources. If you are linking js files using a relative path then the file will obviously get a 404 because its looking for URL path. for example if the URL path is /file/ instead of file.html then your relative resources are loading from /file/ which is not a directory but rewritten html file. To fix this make your links absolute or use base tag. In the header of your webpage add this <base href="/"> so that your relative links can load from the correct location.

Sign up to request clarification or add additional context in comments.

11 Comments

@Jam, Please make sure to clear your browser cache before testing your URLs.
I still have the problem of access from the different levels. I have the URL like this: MyDomain.com/MyCategory/MySubcategory/MyProduct/newlink.html It should be like this: MyDomain.com/newlink.html
@Jam, Could you please do elaborate on this one, what's not working, thank you.
Sure, when I reach the higher levels, everithing is fine e.g. MyDomain.com/MyCategory/MySubcategory or MyDomain.com/MyCategory/MySubcategory/MyProduct But if inside these pages, I try to access to MyDomain.com/newlink.html I get these results: MyDomain.com/MyCategory/MySubcategory/MyProduct/newlink.html or MyDomain.com/MyCategory/MySubcategory/newlink.html or MyDomain.com/MyCategory/newlink.html which is incorrect
@Jam: Looks like, you still don't have <base href="/"> included in your page HTML's head section.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.